

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>cup.res.linux &mdash; cup 1.7.0 documentation</title>
  

  
  
  
  

  

  
  
    

  

  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../../genindex/" />
    <link rel="search" title="Search" href="../../../../search/" /> 

  
  <script src="../../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../../" class="icon icon-home"> cup
          

          
          </a>

          
            
            
              <div class="version">
                1.7
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search/" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../cup/">cup package</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../">cup</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../">Docs</a> &raquo;</li>
        
          <li><a href="../../../">Module code</a> &raquo;</li>
        
      <li>cup.res.linux</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for cup.res.linux</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/bin/env python  pylint: disable=C0302</span>
<span class="c1"># -*- coding: utf-8 -*</span>
<span class="c1"># Authors: Giampaolo Rodola of psutil</span>
<span class="c1">#     The class [Process] is back ported from python open-source project</span>
<span class="c1">#     psutil. Guanna Ma back ported it to py-cup.</span>
<span class="c1">#     If any concern, plz contact mythmgn@gmail.com</span>
<span class="c1">#     Here is the original license applied.</span>
<span class="c1"># :Copyright - (Psutil)</span>
<span class="c1">#     Copyright (c) 2009, Giampaolo Rodola&#39;. All rights reserved.</span>
<span class="c1">#     Use of this source code is governed by a BSD-style license</span>
<span class="c1">#     that can be found in the LICENSE file.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Provie Linux Resource/State Info Query</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">errno</span>
<span class="kn">import</span> <span class="nn">socket</span>
<span class="kn">import</span> <span class="nn">base64</span>
<span class="kn">import</span> <span class="nn">struct</span>
<span class="kn">import</span> <span class="nn">threading</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">import</span> <span class="nn">collections</span>
<span class="kn">from</span> <span class="nn">functools</span> <span class="k">import</span> <span class="n">wraps</span>

<span class="kn">import</span> <span class="nn">cup</span>
<span class="kn">from</span> <span class="nn">cup</span> <span class="k">import</span> <span class="n">decorators</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s1">&#39;boot_time&#39;</span><span class="p">,</span> <span class="s1">&#39;get_boottime_since_epoch&#39;</span><span class="p">,</span>
    <span class="c1"># disk info</span>
    <span class="s1">&#39;get_disk_usage_all&#39;</span><span class="p">,</span> <span class="s1">&#39;get_disk_info&#39;</span><span class="p">,</span>
    <span class="c1"># cpu info</span>
    <span class="s1">&#39;CPUInfo&#39;</span><span class="p">,</span> <span class="s1">&#39;get_cpu_usage&#39;</span><span class="p">,</span> <span class="s1">&#39;get_cpu_nums&#39;</span><span class="p">,</span>
    <span class="s1">&#39;MemInfo&#39;</span><span class="p">,</span> <span class="s1">&#39;get_meminfo&#39;</span><span class="p">,</span>
    <span class="s1">&#39;SWAPINFO&#39;</span><span class="p">,</span> <span class="s1">&#39;get_swapinfo&#39;</span><span class="p">,</span>
    <span class="s1">&#39;net_io_counters&#39;</span><span class="p">,</span> <span class="s1">&#39;get_net_through&#39;</span><span class="p">,</span> <span class="s1">&#39;get_net_transmit_speed&#39;</span><span class="p">,</span>
    <span class="s1">&#39;pids&#39;</span><span class="p">,</span> <span class="s1">&#39;process_iter&#39;</span><span class="p">,</span> <span class="s1">&#39;Process&#39;</span><span class="p">,</span>
    <span class="s1">&#39;get_kernel_version&#39;</span><span class="p">,</span>
<span class="p">]</span>

<span class="n">_CLOCK_TICKS</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">sysconf</span><span class="p">(</span><span class="s2">&quot;SC_CLK_TCK&quot;</span><span class="p">)</span>
<span class="n">_PAGESIZE</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">sysconf</span><span class="p">(</span><span class="s2">&quot;SC_PAGE_SIZE&quot;</span><span class="p">)</span>
<span class="n">_PROC_STATUSES</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s2">&quot;R&quot;</span><span class="p">:</span> <span class="s1">&#39;STATUS_RUNNING&#39;</span><span class="p">,</span>
    <span class="s2">&quot;S&quot;</span><span class="p">:</span> <span class="s1">&#39;STATUS_SLEEPING&#39;</span><span class="p">,</span>
    <span class="s2">&quot;D&quot;</span><span class="p">:</span> <span class="s1">&#39;STATUS_DISK_SLEEP&#39;</span><span class="p">,</span>
    <span class="s2">&quot;T&quot;</span><span class="p">:</span> <span class="s1">&#39;STATUS_STOPPED&#39;</span><span class="p">,</span>
    <span class="s2">&quot;t&quot;</span><span class="p">:</span> <span class="s1">&#39;STATUS_TRACING_STOP&#39;</span><span class="p">,</span>
    <span class="s2">&quot;Z&quot;</span><span class="p">:</span> <span class="s1">&#39;STATUS_ZOMBIE&#39;</span><span class="p">,</span>
    <span class="s2">&quot;X&quot;</span><span class="p">:</span> <span class="s1">&#39;STATUS_DEAD&#39;</span><span class="p">,</span>
    <span class="s2">&quot;x&quot;</span><span class="p">:</span> <span class="s1">&#39;STATUS_DEAD&#39;</span><span class="p">,</span>
    <span class="s2">&quot;K&quot;</span><span class="p">:</span> <span class="s1">&#39;STATUS_WAKE_KILL&#39;</span><span class="p">,</span>
    <span class="s2">&quot;W&quot;</span><span class="p">:</span> <span class="s1">&#39;STATUS_WAKING&#39;</span>
<span class="p">}</span>
<span class="n">_CONN_NONE</span> <span class="o">=</span> <span class="s1">&#39;CONN_NONE&#39;</span>
<span class="n">_TCP_STATUSES</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s2">&quot;01&quot;</span><span class="p">:</span> <span class="s1">&#39;CONN_ESTABLISHED&#39;</span><span class="p">,</span>
    <span class="s2">&quot;02&quot;</span><span class="p">:</span> <span class="s1">&#39;CONN_SYN_SENT&#39;</span><span class="p">,</span>
    <span class="s2">&quot;03&quot;</span><span class="p">:</span> <span class="s1">&#39;CONN_SYN_RECV&#39;</span><span class="p">,</span>
    <span class="s2">&quot;04&quot;</span><span class="p">:</span> <span class="s1">&#39;CONN_FIN_WAIT1&#39;</span><span class="p">,</span>
    <span class="s2">&quot;05&quot;</span><span class="p">:</span> <span class="s1">&#39;CONN_FIN_WAIT2&#39;</span><span class="p">,</span>
    <span class="s2">&quot;06&quot;</span><span class="p">:</span> <span class="s1">&#39;CONN_TIME_WAIT&#39;</span><span class="p">,</span>
    <span class="s2">&quot;07&quot;</span><span class="p">:</span> <span class="s1">&#39;CONN_CLOSE&#39;</span><span class="p">,</span>
    <span class="s2">&quot;08&quot;</span><span class="p">:</span> <span class="s1">&#39;CONN_CLOSE_WAIT&#39;</span><span class="p">,</span>
    <span class="s2">&quot;09&quot;</span><span class="p">:</span> <span class="s1">&#39;CONN_LAST_ACK&#39;</span><span class="p">,</span>
    <span class="s2">&quot;0A&quot;</span><span class="p">:</span> <span class="s1">&#39;CONN_LISTEN&#39;</span><span class="p">,</span>
    <span class="s2">&quot;0B&quot;</span><span class="p">:</span> <span class="s1">&#39;CONN_CLOSING&#39;</span>
<span class="p">}</span>


<span class="c1"># # # # begin system infos # # # #</span>
<div class="viewcode-block" id="get_boottime_since_epoch"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.get_boottime_since_epoch">[docs]</a><span class="k">def</span> <span class="nf">get_boottime_since_epoch</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    :return:</span>
<span class="sd">        return boot time (seconds) since epoch</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">decorators</span><span class="o">.</span><span class="n">needlinux</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">fp</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;/proc/stat&#39;</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">fp</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;btime&#39;</span><span class="p">):</span>
                <span class="k">return</span> <span class="nb">float</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">1</span><span class="p">])</span>
        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;line &#39;btime&#39; not found&quot;</span><span class="p">)</span>
    <span class="k">finally</span><span class="p">:</span>
        <span class="n">fp</span><span class="o">.</span><span class="n">close</span><span class="p">()</span></div>


<div class="viewcode-block" id="get_kernel_version"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.get_kernel_version">[docs]</a><span class="k">def</span> <span class="nf">get_kernel_version</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    get linux kernel verions, e.g.(&#39;2&#39;, &#39;6&#39;, &#39;32&#39;):</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">decorators</span><span class="o">.</span><span class="n">needlinux</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">versions</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">uname</span><span class="p">()[</span><span class="mi">2</span><span class="p">]</span>
    <span class="c1"># version = versions[0: versions.find(&#39;_&#39;)]</span>
    <span class="k">return</span> <span class="nb">tuple</span><span class="p">([</span><span class="n">info</span> <span class="k">for</span> <span class="n">info</span> <span class="ow">in</span> <span class="n">versions</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)])</span></div>


<div class="viewcode-block" id="get_cpu_nums"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.get_cpu_nums">[docs]</a><span class="k">def</span> <span class="nf">get_cpu_nums</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    get cpu nums</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">decorators</span><span class="o">.</span><span class="n">needlinux</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">sysconf</span><span class="p">(</span><span class="s2">&quot;SC_NPROCESSORS_ONLN&quot;</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
        <span class="c1"># as a second fallback we try to parse /proc/cpuinfo</span>
        <span class="n">num</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">fp</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;/proc/cpuinfo&#39;</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">lines</span> <span class="o">=</span> <span class="n">fp</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="n">fp</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;processor&#39;</span><span class="p">):</span>
                <span class="n">num</span> <span class="o">+=</span> <span class="mi">1</span>

    <span class="c1"># unknown format (e.g. amrel/sparc architectures), see:</span>
    <span class="c1"># http://code.google.com/p/psutil/issues/detail?id=200</span>
    <span class="c1"># try to parse /proc/stat as a last resort</span>
    <span class="k">if</span> <span class="n">num</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">fp</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;/proc/stat&#39;</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">lines</span> <span class="o">=</span> <span class="n">fp</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="n">fp</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="n">search</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;cpu\d&#39;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">:</span>
            <span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">search</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">line</span><span class="p">):</span>
                <span class="n">num</span> <span class="o">+=</span> <span class="mi">1</span>

    <span class="k">if</span> <span class="n">num</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;couldn&#39;t determine platform&#39;s NUM_CPUS&quot;</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">num</span></div>


<div class="viewcode-block" id="get_disk_usage_all"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.get_disk_usage_all">[docs]</a><span class="k">def</span> <span class="nf">get_disk_usage_all</span><span class="p">(</span><span class="n">raw</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    :param raw:</span>
<span class="sd">        if raw is True, will use Byte as the measure. Automatically use</span>
<span class="sd">        MB/GB otherwise.</span>

<span class="sd">    :return:</span>
<span class="sd">        return a dict of disk usage</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">decorators</span><span class="o">.</span><span class="n">needlinux</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">byte2gb</span> <span class="o">=</span> <span class="mi">1024</span> <span class="o">*</span> <span class="mi">1024</span> <span class="o">*</span> <span class="mi">1024</span>
    <span class="n">byte2mb</span> <span class="o">=</span> <span class="mi">1024</span> <span class="o">*</span> <span class="mi">1024</span>
    <span class="n">st</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">statvfs</span><span class="p">(</span><span class="s2">&quot;/&quot;</span><span class="p">)</span>
    <span class="n">free</span> <span class="o">=</span> <span class="n">st</span><span class="o">.</span><span class="n">f_bavail</span> <span class="o">*</span> <span class="n">st</span><span class="o">.</span><span class="n">f_frsize</span>
    <span class="n">total</span> <span class="o">=</span> <span class="n">st</span><span class="o">.</span><span class="n">f_blocks</span> <span class="o">*</span> <span class="n">st</span><span class="o">.</span><span class="n">f_frsize</span>
    <span class="n">unit</span> <span class="o">=</span> <span class="s2">&quot;Byte&quot;</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">raw</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">total</span> <span class="o">&gt;</span> <span class="n">byte2gb</span><span class="p">:</span>
            <span class="n">free</span><span class="p">,</span> <span class="n">total</span> <span class="o">=</span> \
                <span class="n">free</span> <span class="o">/</span> <span class="n">byte2gb</span><span class="p">,</span> <span class="n">total</span> <span class="o">/</span> <span class="n">byte2gb</span>
            <span class="n">unit</span> <span class="o">=</span> <span class="s2">&quot;GB&quot;</span>
        <span class="k">elif</span> <span class="n">total</span> <span class="o">&gt;</span> <span class="n">byte2mb</span><span class="p">:</span>
            <span class="n">free</span><span class="p">,</span> <span class="n">total</span> <span class="o">=</span> \
                <span class="n">free</span> <span class="o">/</span> <span class="n">byte2mb</span><span class="p">,</span> <span class="n">total</span> <span class="o">/</span> <span class="n">byte2mb</span>
            <span class="n">unit</span> <span class="o">=</span> <span class="s2">&quot;MB&quot;</span>
    <span class="k">return</span> <span class="p">{</span>
        <span class="s2">&quot;totalSpace&quot;</span><span class="p">:</span> <span class="n">total</span><span class="p">,</span>
        <span class="s2">&quot;usedSpace&quot;</span><span class="p">:</span> <span class="n">total</span> <span class="o">-</span> <span class="n">free</span><span class="p">,</span>
        <span class="s2">&quot;freeSpace&quot;</span><span class="p">:</span> <span class="n">free</span><span class="p">,</span>
        <span class="s2">&quot;unit&quot;</span><span class="p">:</span><span class="n">unit</span>
    <span class="p">}</span></div>


<div class="viewcode-block" id="get_disk_info"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.get_disk_info">[docs]</a><span class="k">def</span> <span class="nf">get_disk_info</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    :return:</span>
<span class="sd">        get disk info of the system</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">decorators</span><span class="o">.</span><span class="n">needlinux</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">info</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">popen</span><span class="p">(</span><span class="s2">&quot;df -lh&quot;</span><span class="p">)</span>
    <span class="n">allDiskInfo</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">info</span><span class="o">.</span><span class="n">readlines</span><span class="p">()):</span>
        <span class="k">if</span> <span class="n">line</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">blockInfo</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">block</span> <span class="ow">in</span> <span class="n">line</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot; &quot;</span><span class="p">):</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">block</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">blockInfo</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">block</span><span class="p">)</span>
            <span class="n">allDiskInfo</span><span class="o">.</span><span class="n">append</span><span class="p">({</span>
                <span class="s2">&quot;FileSystem&quot;</span><span class="p">:</span>  <span class="n">blockInfo</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
                <span class="s2">&quot;Size&quot;</span><span class="p">:</span>        <span class="n">blockInfo</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span>
                <span class="s2">&quot;Used&quot;</span><span class="p">:</span>        <span class="n">blockInfo</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span>
                <span class="s2">&quot;Available&quot;</span><span class="p">:</span>   <span class="n">blockInfo</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span>
                <span class="s2">&quot;Percentage&quot;</span><span class="p">:</span>  <span class="n">blockInfo</span><span class="p">[</span><span class="mi">4</span><span class="p">],</span>
                <span class="p">})</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">continue</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">allDiskInfo</span>
    <span class="k">except</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;couldn&#39;t find disk&quot;</span><span class="p">)</span></div>


<div class="viewcode-block" id="MemInfo"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.MemInfo">[docs]</a><span class="k">class</span> <span class="nc">MemInfo</span><span class="p">(</span><span class="n">collections</span><span class="o">.</span><span class="n">namedtuple</span><span class="p">(</span><span class="s1">&#39;vmem&#39;</span><span class="p">,</span> <span class="s1">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span>
        <span class="c1"># all platforms</span>
        <span class="s1">&#39;total&#39;</span><span class="p">,</span> <span class="s1">&#39;available&#39;</span><span class="p">,</span> <span class="s1">&#39;percent&#39;</span><span class="p">,</span> <span class="s1">&#39;used&#39;</span><span class="p">,</span> <span class="s1">&#39;free&#39;</span><span class="p">,</span>
        <span class="c1"># linux specific</span>
        <span class="s1">&#39;active&#39;</span><span class="p">,</span>
        <span class="s1">&#39;inactive&#39;</span><span class="p">,</span>
        <span class="s1">&#39;buffers&#39;</span><span class="p">,</span>
        <span class="s1">&#39;cached&#39;</span><span class="p">]))):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    get_meminfo will get memory info (a namedtuple returned:</span>
<span class="sd">        total, available, percent, used, free,</span>
<span class="sd">        active,</span>
<span class="sd">        inactive,</span>
<span class="sd">        buffers,</span>
<span class="sd">        cached)</span>

<span class="sd">    E.g.:</span>
<span class="sd">    ::</span>
<span class="sd">        from cup.res import linux</span>
<span class="sd">        meminfo = linux.get_meminfo()</span>
<span class="sd">        print meminfo.total</span>
<span class="sd">        print meminfo.available</span>
<span class="sd">    &quot;&quot;&quot;</span></div>

<span class="c1">#    user</span>
<span class="c1">#       (1) Time spent in user mode.</span>
<span class="c1">#    nice</span>
<span class="c1">#       (2) Time spent in user mode with low priority (nice).</span>
<span class="c1">#    system</span>
<span class="c1">#       (3) Time spent in system mode.</span>
<span class="c1">#    idle</span>
<span class="c1">#       (4) Time spent in the idle task.</span>
<span class="c1">#           This value should be USER_HZ times the</span>
<span class="c1">#           second entry in the /proc/uptime pseudo-file.</span>
<span class="c1">#    iowait (since Linux 2.5.41)</span>
<span class="c1">#       (5) Time waiting for I/O to complete.</span>
<span class="c1">#    irq (since Linux 2.6.0-test4)</span>
<span class="c1">#       (6) Time servicing interrupts.</span>
<span class="c1">#    softirq (since Linux 2.6.0-test4)</span>
<span class="c1">#       (7) Time servicing softirqs.</span>
<span class="c1">#    steal (since Linux 2.6.11)</span>
<span class="c1">#       (8) Stolen time, which is the time spent in other</span>
<span class="c1">#            operating systems when</span>
<span class="c1">#        running in a virtualized environment</span>
<span class="c1">#    guest (since Linux 2.6.24)</span>
<span class="c1">#       (9) Time spent running a virtual CPU for</span>
<span class="c1">#           guest operating systems under the</span>
<span class="c1">#           control of the Linux kernel.</span>
<span class="c1">#    guest_nice (since Linux 2.6.33)</span>
<span class="c1">#       (10) Time spent running a niced guest</span>
<span class="c1">#            (virtual CPU for guest operating systems under the</span>
<span class="c1">#            control of the Linux kernel).</span>
<span class="n">_CPU_COLUMNS</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s1">&#39;usr&#39;</span><span class="p">,</span>
    <span class="s1">&#39;nice&#39;</span><span class="p">,</span>
    <span class="s1">&#39;system&#39;</span><span class="p">,</span>
    <span class="s1">&#39;idle&#39;</span><span class="p">,</span>
    <span class="s1">&#39;iowait&#39;</span><span class="p">,</span>
    <span class="s1">&#39;irq&#39;</span><span class="p">,</span>
    <span class="s1">&#39;softirq&#39;</span><span class="p">,</span>
    <span class="s1">&#39;steal&#39;</span><span class="p">,</span>
    <span class="s1">&#39;guest&#39;</span>
<span class="p">]</span>


<span class="n">_COLUMN_LOCK</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Lock</span><span class="p">()</span>

<span class="n">_COLUMN_LOCK</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>
<span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">platform</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;linux&#39;</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">get_kernel_version</span><span class="p">()</span> <span class="o">&gt;=</span> <span class="p">(</span><span class="s1">&#39;2&#39;</span><span class="p">,</span> <span class="s1">&#39;6&#39;</span><span class="p">,</span> <span class="s1">&#39;33&#39;</span><span class="p">)</span> <span class="ow">and</span> \
            <span class="n">_CPU_COLUMNS</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="s1">&#39;guest_nice&#39;</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">_CPU_COLUMNS</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;guest_nice&#39;</span><span class="p">)</span>
<span class="n">_COLUMN_LOCK</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>


<div class="viewcode-block" id="CPUInfo"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.CPUInfo">[docs]</a><span class="k">class</span> <span class="nc">CPUInfo</span><span class="p">(</span><span class="n">collections</span><span class="o">.</span><span class="n">namedtuple</span><span class="p">(</span><span class="s1">&#39;CPUInfo&#39;</span><span class="p">,</span> <span class="n">_CPU_COLUMNS</span><span class="p">)):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        CPUInfo is used for get_cpu_usage function. The following attr will be</span>
<span class="sd">        in the namedtuple:</span>
<span class="sd">        usr,</span>
<span class="sd">        nice,</span>
<span class="sd">        system,</span>
<span class="sd">        idle,</span>
<span class="sd">        iowait,</span>
<span class="sd">        irq,</span>
<span class="sd">        softirq,</span>
<span class="sd">        steal,</span>
<span class="sd">        guest</span>

<span class="sd">        I.g.</span>
<span class="sd">        ::</span>
<span class="sd">            import cup</span>
<span class="sd">            # count cpu usage</span>
<span class="sd">            from cup.res import linux</span>
<span class="sd">            cpuinfo = linux.get_cpu_usage(intvl_in_sec=60)</span>
<span class="sd">            print cpuinfo.usr</span>
<span class="sd">    &quot;&quot;&quot;</span></div>


<span class="k">def</span> <span class="nf">_get_cput_by_stat</span><span class="p">():</span>
    <span class="n">usr</span> <span class="o">=</span> <span class="n">nice</span> <span class="o">=</span> <span class="n">system</span> <span class="o">=</span> <span class="n">idle</span> <span class="o">=</span> <span class="n">iowait</span> <span class="o">=</span> <span class="n">irq</span> <span class="o">=</span> <span class="n">softirq</span> <span class="o">=</span> <span class="n">steal</span> <span class="o">=</span> \
        <span class="n">guest</span> <span class="o">=</span> <span class="n">guest_nice</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="mf">0.0</span><span class="p">)</span>
    <span class="n">fp</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;/proc/stat&#39;</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">fp</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;cpu&#39;</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">_CPU_COLUMNS</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="s1">&#39;guest_nice&#39;</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="p">(</span><span class="n">usr</span><span class="p">,</span> <span class="n">nice</span><span class="p">,</span> <span class="n">system</span><span class="p">,</span> <span class="n">idle</span><span class="p">,</span> <span class="n">iowait</span><span class="p">,</span> <span class="n">irq</span><span class="p">,</span>
                        <span class="n">softirq</span><span class="p">,</span> <span class="n">steal</span><span class="p">,</span> <span class="n">guest</span><span class="p">,</span> <span class="n">guest_nice</span><span class="p">)</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">1</span><span class="p">:]</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="p">(</span><span class="n">usr</span><span class="p">,</span> <span class="n">nice</span><span class="p">,</span> <span class="n">system</span><span class="p">,</span> <span class="n">idle</span><span class="p">,</span> <span class="n">iowait</span><span class="p">,</span> <span class="n">irq</span><span class="p">,</span>
                        <span class="n">softirq</span><span class="p">,</span> <span class="n">steal</span><span class="p">,</span> <span class="n">guest</span><span class="p">)</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">1</span><span class="p">:]</span>
                <span class="k">break</span>
    <span class="c1"># pylint: disable=W0703</span>
    <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
        <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;Get cpuinfo failed&#39;</span><span class="p">,</span> <span class="ne">RuntimeWarning</span><span class="p">)</span>
    <span class="k">finally</span><span class="p">:</span>
        <span class="n">fp</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

    <span class="k">if</span> <span class="n">_CPU_COLUMNS</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="s1">&#39;guest_nice&#39;</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">CPUInfo</span><span class="p">(</span>
            <span class="n">usr</span><span class="p">,</span> <span class="n">nice</span><span class="p">,</span> <span class="n">system</span><span class="p">,</span> <span class="n">idle</span><span class="p">,</span> <span class="n">iowait</span><span class="p">,</span> <span class="n">irq</span><span class="p">,</span>
            <span class="n">softirq</span><span class="p">,</span> <span class="n">steal</span><span class="p">,</span> <span class="n">guest</span><span class="p">,</span> <span class="n">guest_nice</span>
        <span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">CPUInfo</span><span class="p">(</span>
            <span class="n">usr</span><span class="p">,</span> <span class="n">nice</span><span class="p">,</span> <span class="n">system</span><span class="p">,</span> <span class="n">idle</span><span class="p">,</span> <span class="n">iowait</span><span class="p">,</span> <span class="n">irq</span><span class="p">,</span>
            <span class="n">softirq</span><span class="p">,</span> <span class="n">steal</span><span class="p">,</span> <span class="n">guest</span>
        <span class="p">)</span>


<div class="viewcode-block" id="get_cpu_usage"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.get_cpu_usage">[docs]</a><span class="k">def</span> <span class="nf">get_cpu_usage</span><span class="p">(</span><span class="n">intvl_in_sec</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    get cpu usage statistics during a time period (intvl_in_sec), return a</span>
<span class="sd">    namedtuple CPUInfo</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">decorators</span><span class="o">.</span><span class="n">needlinux</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">cup</span><span class="o">.</span><span class="n">unittest</span><span class="o">.</span><span class="n">assert_ge</span><span class="p">(</span><span class="n">intvl_in_sec</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
    <span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">xrange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">_CPU_COLUMNS</span><span class="p">)):</span>
        <span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
    <span class="n">cpu_info0</span> <span class="o">=</span> <span class="n">_get_cput_by_stat</span><span class="p">()</span>
    <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">intvl_in_sec</span><span class="p">)</span>
    <span class="n">cpu_info1</span> <span class="o">=</span> <span class="n">_get_cput_by_stat</span><span class="p">()</span>
    <span class="n">total</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="mf">0.0</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">xrange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">cpu_info1</span><span class="p">)):</span>
        <span class="n">minus</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">cpu_info1</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="o">-</span> <span class="nb">float</span><span class="p">(</span><span class="n">cpu_info0</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
        <span class="n">total</span> <span class="o">=</span> <span class="n">total</span> <span class="o">+</span> <span class="n">minus</span>
        <span class="n">ret</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">minus</span>

    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">xrange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">ret</span><span class="p">)):</span>
        <span class="n">ret</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="mi">100</span> <span class="o">/</span> <span class="n">total</span>
    <span class="k">return</span> <span class="n">CPUInfo</span><span class="p">(</span><span class="o">*</span><span class="n">ret</span><span class="p">)</span></div>


<div class="viewcode-block" id="get_meminfo"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.get_meminfo">[docs]</a><span class="k">def</span> <span class="nf">get_meminfo</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    get system memory info</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">decorators</span><span class="o">.</span><span class="n">needlinux</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">total</span> <span class="o">=</span> <span class="n">free</span> <span class="o">=</span> <span class="n">buffers</span> <span class="o">=</span> <span class="n">cached</span> <span class="o">=</span> <span class="n">active</span> <span class="o">=</span> <span class="n">inactive</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">fp</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;/proc/meminfo&#39;</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">fp</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;MemTotal&#39;</span><span class="p">):</span>
                <span class="n">total</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">1</span><span class="p">])</span> <span class="o">*</span> <span class="mi">1024</span>
            <span class="k">elif</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;MemFree&#39;</span><span class="p">):</span>
                <span class="n">free</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">1</span><span class="p">])</span> <span class="o">*</span> <span class="mi">1024</span>
            <span class="k">elif</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;Buffers&#39;</span><span class="p">):</span>
                <span class="n">buffers</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">1</span><span class="p">])</span> <span class="o">*</span> <span class="mi">1024</span>
            <span class="k">elif</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;Cached:&#39;</span><span class="p">):</span>
                <span class="n">cached</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">1</span><span class="p">])</span> <span class="o">*</span> <span class="mi">1024</span>
            <span class="k">elif</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;Active:&#39;</span><span class="p">):</span>
                <span class="n">active</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">1</span><span class="p">])</span> <span class="o">*</span> <span class="mi">1024</span>
            <span class="k">elif</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;Inactive:&#39;</span><span class="p">):</span>
                <span class="n">inactive</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">1</span><span class="p">])</span> <span class="o">*</span> <span class="mi">1024</span>
            <span class="k">if</span> <span class="n">cached</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> \
                <span class="ow">and</span> <span class="n">active</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> \
                    <span class="ow">and</span> <span class="n">inactive</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">break</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># we might get here when dealing with exotic Linux flavors, see:</span>
            <span class="c1"># http://code.google.com/p/psutil/issues/detail?id=313</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="s2">&quot;&#39;cached&#39;, &#39;active&#39; and &#39;inactive&#39; memory stats couldn&#39;t &quot;</span> \
                  <span class="s2">&quot;be determined and were set to 0&quot;</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="ne">RuntimeWarning</span><span class="p">)</span>
            <span class="n">total</span> <span class="o">=</span> <span class="n">free</span> <span class="o">=</span> <span class="n">buffers</span> <span class="o">=</span> <span class="n">cached</span> <span class="o">=</span> <span class="n">active</span> <span class="o">=</span> <span class="n">inactive</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">finally</span><span class="p">:</span>
        <span class="n">fp</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
    <span class="n">avail</span> <span class="o">=</span> <span class="n">free</span> <span class="o">+</span> <span class="n">buffers</span> <span class="o">+</span> <span class="n">cached</span>
    <span class="n">used</span> <span class="o">=</span> <span class="n">total</span> <span class="o">-</span> <span class="n">free</span>
    <span class="n">percent</span> <span class="o">=</span> <span class="nb">int</span><span class="p">((</span><span class="n">total</span> <span class="o">-</span> <span class="n">avail</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100</span> <span class="o">/</span> <span class="n">total</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">MemInfo</span><span class="p">(</span>
        <span class="n">total</span><span class="p">,</span>
        <span class="n">avail</span><span class="p">,</span>
        <span class="n">percent</span><span class="p">,</span>
        <span class="n">used</span><span class="p">,</span>
        <span class="n">free</span><span class="p">,</span>
        <span class="n">active</span><span class="p">,</span>
        <span class="n">inactive</span><span class="p">,</span>
        <span class="n">buffers</span><span class="p">,</span>
        <span class="n">cached</span>
    <span class="p">)</span></div>


<div class="viewcode-block" id="SWAPINFO"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.SWAPINFO">[docs]</a><span class="k">class</span> <span class="nc">SWAPINFO</span><span class="p">(</span>
    <span class="n">collections</span><span class="o">.</span><span class="n">namedtuple</span><span class="p">(</span>
        <span class="s1">&#39;SwapInfo&#39;</span><span class="p">,</span>
        <span class="p">[</span>
            <span class="s1">&#39;total&#39;</span><span class="p">,</span>
            <span class="s1">&#39;free&#39;</span><span class="p">,</span>
            <span class="s1">&#39;used&#39;</span><span class="p">,</span>
            <span class="s1">&#39;sin&#39;</span><span class="p">,</span>
            <span class="s1">&#39;sout&#39;</span>
        <span class="p">]</span>
    <span class="p">)</span>
<span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    get_swapinfo will return a SWAPINFO</span>
<span class="sd">    &quot;&quot;&quot;</span></div>


<div class="viewcode-block" id="get_swapinfo"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.get_swapinfo">[docs]</a><span class="k">def</span> <span class="nf">get_swapinfo</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    get swamp info of the system</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">decorators</span><span class="o">.</span><span class="n">needlinux</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">fp</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;/proc/swaps&#39;</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>
    <span class="n">reg</span> <span class="o">=</span> <span class="s1">&#39;([</span><span class="se">\\</span><span class="s1">/A-Za-z0-9]+)[</span><span class="se">\\</span><span class="s1">s]+([a-z]+)[</span><span class="se">\\</span><span class="s1">s]+([0-9]+)&#39;</span>\
        <span class="s1">&#39;[</span><span class="se">\\</span><span class="s1">s]+([0-9]+)[</span><span class="se">\\</span><span class="s1">s]+([</span><span class="se">\\</span><span class="s1">-0-9]+).*&#39;</span>
    <span class="n">regobj</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="n">reg</span><span class="p">)</span>
    <span class="n">total</span> <span class="o">=</span> <span class="n">used</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">fp</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">regobj</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">line</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">sp</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
                <span class="n">total</span> <span class="o">+=</span> <span class="nb">int</span><span class="p">(</span><span class="n">sp</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
                <span class="n">used</span> <span class="o">+=</span> <span class="nb">int</span><span class="p">(</span><span class="n">sp</span><span class="p">[</span><span class="mi">3</span><span class="p">])</span>
    <span class="k">finally</span><span class="p">:</span>
        <span class="n">fp</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

    <span class="k">if</span> <span class="n">total</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">total</span> <span class="o">=</span> <span class="n">used</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">msg</span> <span class="o">=</span> <span class="s1">&#39;Failed to get total from /proc/swaps or &#39;</span>\
            <span class="s1">&#39;the system does not have swap mounted.&#39;</span> \
            <span class="s1">&#39; Total and used were set to 0&#39;</span>
        <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="ne">RuntimeWarning</span><span class="p">)</span>

    <span class="n">sin</span> <span class="o">=</span> <span class="n">sout</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">fp</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;/proc/vmstat&#39;</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">fp</span><span class="p">:</span>
            <span class="c1"># values are expressed in 4 kilo bytes, we want bytes instead</span>
            <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;pswpin&#39;</span><span class="p">):</span>
                <span class="n">sin</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">)[</span><span class="mi">1</span><span class="p">])</span> <span class="o">*</span> <span class="mi">4</span> <span class="o">*</span> <span class="mi">1024</span>
            <span class="k">elif</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;pswpout&#39;</span><span class="p">):</span>
                <span class="n">sout</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">)[</span><span class="mi">1</span><span class="p">])</span> <span class="o">*</span> <span class="mi">4</span> <span class="o">*</span> <span class="mi">1024</span>
            <span class="k">if</span> <span class="n">sin</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">sout</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">break</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># we might get here when dealing with exotic Linux flavors, see:</span>
            <span class="c1"># http://code.google.com/p/psutil/issues/detail?id=313</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="s2">&quot;&#39;sin&#39; and &#39;sout&#39; swap memory stats couldn&#39;t &quot;</span> \
                  <span class="s2">&quot;be determined and were set to 0&quot;</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="ne">RuntimeWarning</span><span class="p">)</span>
            <span class="n">sin</span> <span class="o">=</span> <span class="n">sout</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">finally</span><span class="p">:</span>
        <span class="n">fp</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
    <span class="n">free</span> <span class="o">=</span> <span class="n">total</span> <span class="o">-</span> <span class="n">used</span>

    <span class="k">return</span> <span class="n">SWAPINFO</span><span class="p">(</span><span class="n">total</span><span class="p">,</span> <span class="n">free</span><span class="p">,</span> <span class="n">used</span><span class="p">,</span> <span class="n">sin</span><span class="p">,</span> <span class="n">sout</span><span class="p">)</span></div>


<div class="viewcode-block" id="net_io_counters"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.net_io_counters">[docs]</a><span class="k">def</span> <span class="nf">net_io_counters</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    get network statistics with a list of namedtuple</span>
<span class="sd">    (bytes_sent, bytes_recv, packets_sent, packets_recv,</span>
<span class="sd">                         errin, errout, dropin, dropout)</span>
<span class="sd">    example</span>
<span class="sd">    ::</span>
<span class="sd">       {</span>
<span class="sd">           &#39;lo&#39;:</span>
<span class="sd">            (</span>
<span class="sd">                235805206817, 235805206817, 315060887, 315060887, 0, 0, 0, 0</span>
<span class="sd">            ),</span>
<span class="sd">            &#39;eth1&#39;:</span>
<span class="sd">            (</span>
<span class="sd">                18508976300272, 8079464483699, 32776530804,</span>
<span class="sd">                32719666038, 0, 0, 708015, 0</span>
<span class="sd">            ),</span>
<span class="sd">            &#39;eth0&#39;:</span>
<span class="sd">            (</span>
<span class="sd">                0, 0, 0, 0, 0, 0, 0, 0</span>
<span class="sd">            )</span>
<span class="sd">        }</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">decorators</span><span class="o">.</span><span class="n">needlinux</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">fhandle</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;/proc/net/dev&quot;</span><span class="p">,</span> <span class="s2">&quot;r&quot;</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">lines</span> <span class="o">=</span> <span class="n">fhandle</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>
    <span class="k">finally</span><span class="p">:</span>
        <span class="n">fhandle</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
    <span class="n">retdict</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">[</span><span class="mi">2</span><span class="p">:]:</span>
        <span class="n">colon</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">rfind</span><span class="p">(</span><span class="s1">&#39;:&#39;</span><span class="p">)</span>
        <span class="k">assert</span> <span class="n">colon</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">,</span> <span class="nb">repr</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
        <span class="n">name</span> <span class="o">=</span> <span class="n">line</span><span class="p">[:</span><span class="n">colon</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="n">line</span><span class="p">[</span><span class="n">colon</span> <span class="o">+</span> <span class="mi">1</span><span class="p">:]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
        <span class="n">bytes_recv</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">fields</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
        <span class="n">packets_recv</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">fields</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="n">errin</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">fields</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
        <span class="n">dropin</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">fields</span><span class="p">[</span><span class="mi">3</span><span class="p">])</span>
        <span class="n">bytes_sent</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">fields</span><span class="p">[</span><span class="mi">8</span><span class="p">])</span>
        <span class="n">packets_sent</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">fields</span><span class="p">[</span><span class="mi">9</span><span class="p">])</span>
        <span class="n">errout</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">fields</span><span class="p">[</span><span class="mi">10</span><span class="p">])</span>
        <span class="n">dropout</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">fields</span><span class="p">[</span><span class="mi">11</span><span class="p">])</span>
        <span class="n">retdict</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">bytes_sent</span><span class="p">,</span> <span class="n">bytes_recv</span><span class="p">,</span> <span class="n">packets_sent</span><span class="p">,</span> <span class="n">packets_recv</span><span class="p">,</span>
                         <span class="n">errin</span><span class="p">,</span> <span class="n">errout</span><span class="p">,</span> <span class="n">dropin</span><span class="p">,</span> <span class="n">dropout</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">retdict</span></div>


<div class="viewcode-block" id="get_net_through"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.get_net_through">[docs]</a><span class="k">def</span> <span class="nf">get_net_through</span><span class="p">(</span><span class="n">str_interface</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    get network interface statistics by a interface (eth0, e,g,)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">decorators</span><span class="o">.</span><span class="n">needlinux</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">rx_bytes</span> <span class="o">=</span> <span class="n">tx_bytes</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
    <span class="n">fp</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;/proc/net/dev&#39;</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">fp</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">str_interface</span> <span class="ow">in</span> <span class="n">line</span><span class="p">:</span>
                <span class="n">data</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">:&#39;</span> <span class="o">%</span> <span class="n">str_interface</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
                <span class="n">rx_bytes</span><span class="p">,</span> <span class="n">tx_bytes</span> <span class="o">=</span> <span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">data</span><span class="p">[</span><span class="mi">8</span><span class="p">])</span>
    <span class="k">finally</span><span class="p">:</span>
        <span class="n">fp</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
    <span class="k">if</span> <span class="n">rx_bytes</span> <span class="o">&lt;</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">tx_bytes</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">msg</span> <span class="o">=</span> <span class="s1">&#39;Failed to parse /proc/net/dev&#39;</span>
        <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="ne">RuntimeWarning</span><span class="p">)</span>
    <span class="n">cup</span><span class="o">.</span><span class="n">unittest</span><span class="o">.</span><span class="n">assert_ge</span><span class="p">(</span><span class="n">rx_bytes</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
    <span class="n">cup</span><span class="o">.</span><span class="n">unittest</span><span class="o">.</span><span class="n">assert_ge</span><span class="p">(</span><span class="n">tx_bytes</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
    <span class="k">return</span> <span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">rx_bytes</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="n">tx_bytes</span><span class="p">))</span></div>


<div class="viewcode-block" id="get_net_transmit_speed"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.get_net_transmit_speed">[docs]</a><span class="k">def</span> <span class="nf">get_net_transmit_speed</span><span class="p">(</span><span class="n">str_interface</span><span class="p">,</span> <span class="n">intvl_in_sec</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    get network interface write/read speed</span>

<span class="sd">    E.g.</span>
<span class="sd">    ::</span>
<span class="sd">        import cup</span>
<span class="sd">        print cup.res.linux.get_net_transmit_speed(&#39;eth1&#39;, 5)</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">decorators</span><span class="o">.</span><span class="n">needlinux</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">cup</span><span class="o">.</span><span class="n">unittest</span><span class="o">.</span><span class="n">assert_gt</span><span class="p">(</span><span class="n">intvl_in_sec</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
    <span class="n">rx_bytes0</span> <span class="o">=</span> <span class="n">get_net_through</span><span class="p">(</span><span class="n">str_interface</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
    <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">intvl_in_sec</span><span class="p">)</span>
    <span class="n">rx_bytes1</span> <span class="o">=</span> <span class="n">get_net_through</span><span class="p">(</span><span class="n">str_interface</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">rx_bytes1</span> <span class="o">-</span> <span class="n">rx_bytes0</span><span class="p">)</span> <span class="o">/</span> <span class="n">intvl_in_sec</span></div>



<span class="k">def</span> <span class="nf">get_net_recv_speed</span><span class="p">(</span><span class="n">str_interface</span><span class="p">,</span> <span class="n">intvl_in_sec</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    get average network recv-speed during a time period (intvl_in_sec)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">decorators</span><span class="o">.</span><span class="n">needlinux</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">cup</span><span class="o">.</span><span class="n">unittest</span><span class="o">.</span><span class="n">assert_gt</span><span class="p">(</span><span class="n">intvl_in_sec</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
    <span class="n">tx_bytes0</span> <span class="o">=</span> <span class="n">get_net_through</span><span class="p">(</span><span class="n">str_interface</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span>
    <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">intvl_in_sec</span><span class="p">)</span>
    <span class="n">tx_bytes1</span> <span class="o">=</span> <span class="n">get_net_through</span><span class="p">(</span><span class="n">str_interface</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">tx_bytes1</span> <span class="o">-</span> <span class="n">tx_bytes0</span><span class="p">)</span> <span class="o">/</span> <span class="n">intvl_in_sec</span>


<span class="k">def</span> <span class="nf">wrap_exceptions</span><span class="p">(</span><span class="n">fun</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Decorator which translates bare OSError and IOError exceptions</span>
<span class="sd">    into cup.err.NoSuchProcess and cup.err.AccessDenied.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="nd">@wraps</span><span class="p">(</span><span class="n">fun</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">wrapper</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        internal wrapper for wrap_exceptions</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">fun</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">EnvironmentError</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="c1"># ENOENT (no such file or directory) gets raised on open().</span>
            <span class="c1"># ESRCH (no such process) can get raised on read() if</span>
            <span class="c1"># process is gone in meantime.</span>
            <span class="n">err</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">()[</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">err</span><span class="o">.</span><span class="n">errno</span> <span class="ow">in</span> <span class="p">(</span><span class="n">errno</span><span class="o">.</span><span class="n">ENOENT</span><span class="p">,</span> <span class="n">errno</span><span class="o">.</span><span class="n">ESRCH</span><span class="p">):</span>
                <span class="c1"># pylint: disable=W0212</span>
                <span class="k">raise</span> <span class="n">cup</span><span class="o">.</span><span class="n">err</span><span class="o">.</span><span class="n">NoSuchProcess</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_process_name</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">err</span><span class="o">.</span><span class="n">errno</span> <span class="ow">in</span> <span class="p">(</span><span class="n">errno</span><span class="o">.</span><span class="n">EPERM</span><span class="p">,</span> <span class="n">errno</span><span class="o">.</span><span class="n">EACCES</span><span class="p">):</span>
                <span class="k">raise</span> <span class="n">cup</span><span class="o">.</span><span class="n">err</span><span class="o">.</span><span class="n">ResException</span><span class="p">(</span><span class="s1">&#39;EPERM or EACCES&#39;</span><span class="p">)</span>
            <span class="k">raise</span> <span class="n">error</span>
    <span class="k">return</span> <span class="n">wrapper</span>


<span class="n">CLOCK_TICKS</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">sysconf</span><span class="p">(</span><span class="s2">&quot;SC_CLK_TCK&quot;</span><span class="p">)</span>
<div class="viewcode-block" id="boot_time"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.boot_time">[docs]</a><span class="k">def</span> <span class="nf">boot_time</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;Return the system boot time expressed in seconds since the epoch.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">decorators</span><span class="o">.</span><span class="n">needlinux</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;/proc/stat&#39;</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;btime&#39;</span><span class="p">):</span>
                <span class="n">ret</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">1</span><span class="p">])</span>
                <span class="k">return</span> <span class="n">ret</span>
        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;line &#39;btime&#39; not found in /proc/stat&quot;</span><span class="p">)</span></div>


<div class="viewcode-block" id="pids"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.pids">[docs]</a><span class="k">def</span> <span class="nf">pids</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;Returns a list of PIDs currently running on the system.&quot;&quot;&quot;</span>
    <span class="n">decorators</span><span class="o">.</span><span class="n">needlinux</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
    <span class="k">return</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;/proc&#39;</span><span class="p">)</span> <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">isdigit</span><span class="p">()]</span></div>

<span class="n">_pmap</span> <span class="o">=</span> <span class="p">{}</span>


<div class="viewcode-block" id="process_iter"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.process_iter">[docs]</a><span class="k">def</span> <span class="nf">process_iter</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;Return a generator yielding a Process instance for all</span>
<span class="sd">    running processes.</span>

<span class="sd">    Every new Process instance is only created once and then cached</span>
<span class="sd">    into an internal table which is updated every time this is used.</span>

<span class="sd">    Cached Process instances are checked for identity so that you&#39;re</span>
<span class="sd">    safe in case a PID has been reused by another process, in which</span>
<span class="sd">    case the cached instance is updated.</span>

<span class="sd">    yuetian:</span>
<span class="sd">    1. the origion use a check_running function to check whether</span>
<span class="sd">    PID has been reused by another process in which case yield a new</span>
<span class="sd">    Process instance</span>
<span class="sd">    hint:i did not use check_running function because the container of</span>
<span class="sd">    pid is set</span>

<span class="sd">    2. the origion use a sorted list(_pmap.items()) +</span>
<span class="sd">        list(dict.fromkeys(new_pids).items()</span>
<span class="sd">    to get pid and proc to make res.proc is only a instance of a pid Process</span>
<span class="sd">    hint(bugs):i did not use fromkeys(new_pids) because i did not get the meanning</span>
<span class="sd">    of using proc</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">decorators</span><span class="o">.</span><span class="n">needlinux</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">pid_set</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">pids</span><span class="p">())</span>
    <span class="k">for</span> <span class="n">pid</span> <span class="ow">in</span> <span class="n">pid_set</span><span class="p">:</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">check_process</span> <span class="o">=</span> <span class="n">Process</span><span class="p">(</span><span class="n">pid</span><span class="p">)</span>
            <span class="n">res</span> <span class="o">=</span> <span class="n">check_process</span><span class="o">.</span><span class="n">get_process_name</span><span class="p">()</span>
        <span class="k">except</span> <span class="n">cup</span><span class="o">.</span><span class="n">err</span><span class="o">.</span><span class="n">NoSuchProcess</span><span class="p">:</span>
            <span class="k">pass</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">yield</span> <span class="n">check_process</span></div>


<span class="c1"># pylint: disable=R0904</span>
<div class="viewcode-block" id="Process"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.Process">[docs]</a><span class="k">class</span> <span class="nc">Process</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Process info query (given a pid)</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;_pid&quot;</span><span class="p">,</span> <span class="s2">&quot;_process_name&quot;</span><span class="p">,</span> <span class="s2">&quot;_create_time&quot;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">pid</span><span class="p">):</span>
        <span class="n">decorators</span><span class="o">.</span><span class="n">needlinux</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span> <span class="o">=</span> <span class="n">pid</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_process_name</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_create_time</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">lexists</span><span class="p">(</span><span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/exe&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">):</span>
            <span class="k">raise</span> <span class="n">cup</span><span class="o">.</span><span class="n">err</span><span class="o">.</span><span class="n">NoSuchProcess</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_process_name</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_create_time</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">create_time</span><span class="p">()</span>

<div class="viewcode-block" id="Process.create_time"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.Process.create_time">[docs]</a>    <span class="k">def</span> <span class="nf">create_time</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;return -1 represent the process does not exists</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1">#if not os.path.lexists(&quot;/proc/%s/exe&quot; % self._pid):</span>
        <span class="c1">#    raise cup.err.NoSuchProcess(self._pid, self._process_name)</span>
        <span class="c1">#else:</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/stat&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
                <span class="n">st</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="k">except</span> <span class="ne">IOError</span><span class="p">:</span>
            <span class="k">return</span> <span class="o">-</span><span class="mi">1</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">st</span> <span class="o">=</span> <span class="n">st</span><span class="p">[</span><span class="n">st</span><span class="o">.</span><span class="n">rfind</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;)&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="mi">2</span><span class="p">:]</span>
            <span class="n">st</span> <span class="o">=</span> <span class="n">st</span><span class="p">[</span><span class="n">st</span><span class="o">.</span><span class="n">rfind</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;)&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="mi">2</span><span class="p">:]</span>
            <span class="n">values</span> <span class="o">=</span> <span class="n">st</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39; &#39;</span><span class="p">)</span>
            <span class="n">bt</span> <span class="o">=</span> <span class="n">boot_time</span><span class="p">()</span>
            <span class="k">return</span> <span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">values</span><span class="p">[</span><span class="mi">19</span><span class="p">])</span> <span class="o">/</span> <span class="n">CLOCK_TICKS</span><span class="p">)</span> <span class="o">+</span> <span class="n">bt</span></div>

<div class="viewcode-block" id="Process.children"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.Process.children">[docs]</a>    <span class="k">def</span> <span class="nf">children</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">recursive</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the children of this process as a list of Process</span>
<span class="sd">        instances, pre-emptively checking whether PID has been reused.</span>
<span class="sd">        If recursive is True return all the parent descendants.</span>

<span class="sd">        Example (A == this process):</span>

<span class="sd">         A ─┐</span>
<span class="sd">            │</span>
<span class="sd">            ├─ B (child) ─┐</span>
<span class="sd">            │             └─ X (grandchild) ─┐</span>
<span class="sd">            │                                └─ Y (great grandchild)</span>
<span class="sd">            ├─ C (child)</span>
<span class="sd">            └─ D (child)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">ppid_map</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">recursive</span><span class="p">:</span>
            <span class="c1"># &#39;slow&#39; version, common to all platforms except Windows</span>
            <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">process_iter</span><span class="p">():</span>
                <span class="c1">#try:</span>
                <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">get_process_ppid</span><span class="p">()</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">:</span>
                    <span class="c1"># if child happens to be older than its parent</span>
                    <span class="c1"># (self) it means child&#39;s PID has been reused</span>
                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">create_time</span><span class="p">()</span> <span class="o">&lt;=</span> <span class="n">p</span><span class="o">.</span><span class="n">create_time</span><span class="p">():</span>
                        <span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
                <span class="c1">#except (cup.err.NoSuchProcess):</span>
                <span class="c1">#    pass</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># construct a dict where &#39;values&#39; are all the processes</span>
            <span class="c1"># having &#39;key&#39; as their parent</span>
            <span class="n">table</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">defaultdict</span><span class="p">(</span><span class="nb">list</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">process_iter</span><span class="p">():</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">table</span><span class="p">[</span><span class="n">p</span><span class="o">.</span><span class="n">get_process_ppid</span><span class="p">()]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
                <span class="k">except</span> <span class="p">(</span><span class="n">cup</span><span class="o">.</span><span class="n">err</span><span class="o">.</span><span class="n">NoSuchProcess</span><span class="p">):</span>
                    <span class="k">pass</span>
            <span class="c1"># At this point we have a mapping table where table[self.pid]</span>
            <span class="c1"># are the current process&#39; children.</span>
            <span class="c1"># Below, we look for all descendants recursively, similarly</span>
            <span class="c1"># to a recursive function call.</span>
            <span class="n">checkpids</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">]</span>
            <span class="k">for</span> <span class="n">pid</span> <span class="ow">in</span> <span class="n">checkpids</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">child</span> <span class="ow">in</span> <span class="n">table</span><span class="p">[</span><span class="n">pid</span><span class="p">]:</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="c1"># if child happens to be older than its parent</span>
                        <span class="c1"># (self) it means child&#39;s PID has been reused</span>
                        <span class="n">intime</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">create_time</span><span class="p">()</span> <span class="o">&lt;=</span> <span class="n">child</span><span class="o">.</span><span class="n">create_time</span><span class="p">()</span>
                    <span class="k">except</span> <span class="p">(</span><span class="n">cup</span><span class="o">.</span><span class="n">err</span><span class="o">.</span><span class="n">NoSuchProcess</span><span class="p">):</span>
                        <span class="k">pass</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="k">if</span> <span class="n">intime</span><span class="p">:</span>
                            <span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">child</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
                            <span class="k">if</span> <span class="n">child</span><span class="o">.</span><span class="n">_pid</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">checkpids</span><span class="p">:</span>
                                <span class="n">checkpids</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">child</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">ret</span></div>

<div class="viewcode-block" id="Process.get_process_name"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.Process.get_process_name">[docs]</a>    <span class="nd">@wrap_exceptions</span>
    <span class="k">def</span> <span class="nf">get_process_name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get process name of the process (for daemon process only)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">fhandle</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/stat&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">name</span> <span class="o">=</span> <span class="n">fhandle</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;(&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span>
                <span class="s1">&#39;)&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span>
            <span class="p">)</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="n">fhandle</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">name</span></div>

<div class="viewcode-block" id="Process.get_process_exe"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.Process.get_process_exe">[docs]</a>    <span class="k">def</span> <span class="nf">get_process_exe</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get executable info of the process. If the process is a daemon, use</span>
<span class="sd">        get_process_name instead!</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">exe</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">readlink</span><span class="p">(</span><span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/exe&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
        <span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="ne">IOError</span><span class="p">)</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="n">err</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">()[</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">err</span><span class="o">.</span><span class="n">errno</span> <span class="o">==</span> <span class="n">errno</span><span class="o">.</span><span class="n">ENOENT</span><span class="p">:</span>
                <span class="c1"># no such file error; might be raised also if the</span>
                <span class="c1"># path actually exists for system processes with</span>
                <span class="c1"># low pids (about 0-20)</span>
                <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">lexists</span><span class="p">(</span><span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/exe&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">):</span>
                    <span class="k">return</span> <span class="s2">&quot;&quot;</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="c1"># ok, it is a process which has gone away</span>
                    <span class="k">raise</span> <span class="n">cup</span><span class="o">.</span><span class="n">err</span><span class="o">.</span><span class="n">NoSuchProcess</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_process_name</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">err</span><span class="o">.</span><span class="n">errno</span> <span class="ow">in</span> <span class="p">(</span><span class="n">errno</span><span class="o">.</span><span class="n">EPERM</span><span class="p">,</span> <span class="n">errno</span><span class="o">.</span><span class="n">EACCES</span><span class="p">):</span>
                <span class="k">raise</span> <span class="n">cup</span><span class="o">.</span><span class="n">err</span><span class="o">.</span><span class="n">AccessDenied</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_process_name</span><span class="p">)</span>
            <span class="k">raise</span> <span class="n">error</span>

        <span class="c1"># readlink() might return paths containing null bytes causing</span>
        <span class="c1"># problems when used with other fs-related functions (os.*,</span>
        <span class="c1"># open(), ...)</span>
        <span class="n">exe</span> <span class="o">=</span> <span class="n">exe</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\x00</span><span class="s1">&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
        <span class="c1"># Certain names have &#39; (deleted)&#39; appended. Usually this is</span>
        <span class="c1"># bogus as the file actually exists. Either way that&#39;s not</span>
        <span class="c1"># important as we don&#39;t want to discriminate executables which</span>
        <span class="c1"># have been deleted.</span>
        <span class="k">if</span> <span class="n">exe</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s2">&quot; (deleted)&quot;</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">exe</span><span class="p">):</span>
            <span class="n">exe</span> <span class="o">=</span> <span class="n">exe</span><span class="p">[:</span><span class="o">-</span><span class="mi">10</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">exe</span></div>

<div class="viewcode-block" id="Process.get_process_cmdline"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.Process.get_process_cmdline">[docs]</a>    <span class="nd">@wrap_exceptions</span>
    <span class="k">def</span> <span class="nf">get_process_cmdline</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get cmdline</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">fhandle</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/cmdline&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="c1"># return the args as a list</span>
            <span class="k">return</span> <span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">fhandle</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\x00</span><span class="s1">&#39;</span><span class="p">)</span> <span class="k">if</span> <span class="n">x</span><span class="p">]</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="n">fhandle</span><span class="o">.</span><span class="n">close</span><span class="p">()</span></div>

    <span class="n">__nt_io</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">namedtuple</span><span class="p">(</span>
        <span class="s1">&#39;nt_io&#39;</span><span class="p">,</span>
        <span class="p">[</span>
            <span class="s1">&#39;rcount&#39;</span><span class="p">,</span>
            <span class="s1">&#39;wcount&#39;</span><span class="p">,</span>
            <span class="s1">&#39;rbytes&#39;</span><span class="p">,</span>
            <span class="s1">&#39;wbytes&#39;</span>
        <span class="p">]</span>
    <span class="p">)</span>

<div class="viewcode-block" id="Process.get_process_io_counters"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.Process.get_process_io_counters">[docs]</a>    <span class="nd">@wrap_exceptions</span>
    <span class="k">def</span> <span class="nf">get_process_io_counters</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get io statistics info of network adapters.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s1">&#39;/proc/</span><span class="si">%s</span><span class="s1">/io&#39;</span> <span class="o">%</span> <span class="n">os</span><span class="o">.</span><span class="n">getpid</span><span class="p">()):</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;couldn&#39;t find /proc/</span><span class="si">%s</span><span class="s2">/io (kernel &quot;</span>
                                      <span class="s2">&quot;too old?)&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
        <span class="n">fname</span> <span class="o">=</span> <span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/io&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span>
        <span class="c1"># pylint: disable=c0103</span>
        <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">fname</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">rcount</span> <span class="o">=</span> <span class="n">wcount</span> <span class="o">=</span> <span class="n">rbytes</span> <span class="o">=</span> <span class="n">wbytes</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">rcount</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;syscr&quot;</span><span class="p">):</span>
                    <span class="n">rcount</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">1</span><span class="p">])</span>
                <span class="k">elif</span> <span class="n">wcount</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;syscw&quot;</span><span class="p">):</span>
                    <span class="n">wcount</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">1</span><span class="p">])</span>
                <span class="k">elif</span> <span class="n">rbytes</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;read_bytes&quot;</span><span class="p">):</span>
                    <span class="n">rbytes</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">1</span><span class="p">])</span>
                <span class="k">elif</span> <span class="n">wbytes</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;write_bytes&quot;</span><span class="p">):</span>
                    <span class="n">wbytes</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">1</span><span class="p">])</span>
            <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="p">(</span><span class="n">rcount</span><span class="p">,</span> <span class="n">wcount</span><span class="p">,</span> <span class="n">rbytes</span><span class="p">,</span> <span class="n">wbytes</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">_</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span>
                        <span class="s2">&quot;couldn&#39;t read all necessary info from </span><span class="si">%r</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">fname</span><span class="p">)</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__nt_io</span><span class="p">(</span><span class="n">rcount</span><span class="p">,</span> <span class="n">wcount</span><span class="p">,</span> <span class="n">rbytes</span><span class="p">,</span> <span class="n">wbytes</span><span class="p">)</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span></div>

    <span class="n">_nt_cputimes</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">namedtuple</span><span class="p">(</span>
        <span class="s1">&#39;nt_cputimes&#39;</span><span class="p">,</span>
        <span class="p">[</span>
            <span class="s1">&#39;utime&#39;</span><span class="p">,</span>
            <span class="s1">&#39;stime&#39;</span>
        <span class="p">]</span>
    <span class="p">)</span>

<div class="viewcode-block" id="Process.get_cpu_times"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.Process.get_cpu_times">[docs]</a>    <span class="nd">@wrap_exceptions</span>
    <span class="k">def</span> <span class="nf">get_cpu_times</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get cpu times, return with a namedtuple (utime, stime)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/stat&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">st</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="c1"># ignore the first two values (&quot;pid (exe)&quot;)</span>
        <span class="n">st</span> <span class="o">=</span> <span class="n">st</span><span class="p">[</span><span class="n">st</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;)&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="mi">2</span><span class="p">:]</span>
        <span class="n">values</span> <span class="o">=</span> <span class="n">st</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">)</span>
        <span class="n">utime</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">values</span><span class="p">[</span><span class="mi">11</span><span class="p">])</span> <span class="o">/</span> <span class="n">_CLOCK_TICKS</span>
        <span class="n">stime</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">values</span><span class="p">[</span><span class="mi">12</span><span class="p">])</span> <span class="o">/</span> <span class="n">_CLOCK_TICKS</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_nt_cputimes</span><span class="p">(</span><span class="n">utime</span><span class="p">,</span> <span class="n">stime</span><span class="p">)</span></div>

<div class="viewcode-block" id="Process.get_process_create_time"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.Process.get_process_create_time">[docs]</a>    <span class="nd">@wrap_exceptions</span>
    <span class="k">def</span> <span class="nf">get_process_create_time</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get process create time</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/stat&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">st</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="c1"># ignore the first two values (&quot;pid (exe)&quot;)</span>
        <span class="n">st</span> <span class="o">=</span> <span class="n">st</span><span class="p">[</span><span class="n">st</span><span class="o">.</span><span class="n">rfind</span><span class="p">(</span><span class="s1">&#39;)&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="mi">2</span><span class="p">:]</span>
        <span class="n">values</span> <span class="o">=</span> <span class="n">st</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">)</span>
        <span class="c1"># According to documentation, starttime is in field 21 and the</span>
        <span class="c1"># unit is jiffies (clock ticks).</span>
        <span class="c1"># We first divide it for clock ticks and then add uptime returning</span>
        <span class="c1"># seconds since the epoch, in UTC.</span>
        <span class="n">starttime</span> <span class="o">=</span> <span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">values</span><span class="p">[</span><span class="mi">19</span><span class="p">])</span> <span class="o">/</span> <span class="n">_CLOCK_TICKS</span><span class="p">)</span> <span class="o">+</span> \
            <span class="n">get_boottime_since_epoch</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">starttime</span></div>

    <span class="n">_nt_meminfo</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">namedtuple</span><span class="p">(</span>
        <span class="s1">&#39;nt_meminfo&#39;</span><span class="p">,</span>
        <span class="p">[</span>
            <span class="s1">&#39;rss&#39;</span><span class="p">,</span>
            <span class="s1">&#39;vms&#39;</span>
        <span class="p">]</span>
    <span class="p">)</span>

<div class="viewcode-block" id="Process.get_memory_info"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.Process.get_memory_info">[docs]</a>    <span class="nd">@wrap_exceptions</span>
    <span class="k">def</span> <span class="nf">get_memory_info</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get memory info, return with a namedtuple (</span>
<span class="sd">        rss vms shared text lib data dirty)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/statm&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">vms</span><span class="p">,</span> <span class="n">rss</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">()[:</span><span class="mi">2</span><span class="p">]</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_nt_meminfo</span><span class="p">(</span>
                <span class="nb">int</span><span class="p">(</span><span class="n">rss</span><span class="p">)</span> <span class="o">*</span> <span class="n">_PAGESIZE</span><span class="p">,</span>
                <span class="nb">int</span><span class="p">(</span><span class="n">vms</span><span class="p">)</span> <span class="o">*</span> <span class="n">_PAGESIZE</span>
            <span class="p">)</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span></div>

    <span class="n">_nt_ext_mem</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">namedtuple</span><span class="p">(</span>
        <span class="s1">&#39;meminfo&#39;</span><span class="p">,</span>
        <span class="s1">&#39;rss vms shared text lib data dirty&#39;</span>
    <span class="p">)</span>

<div class="viewcode-block" id="Process.get_ext_memory_info"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.Process.get_ext_memory_info">[docs]</a>    <span class="nd">@wrap_exceptions</span>
    <span class="k">def</span> <span class="nf">get_ext_memory_info</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        return namedtuple with FIELDs below:</span>

<span class="sd">        Example: ::</span>

<span class="sd">            from cup.res import linux</span>
<span class="sd">            process = linux.Process(pid)</span>
<span class="sd">            print process.rss, process.dirty</span>

<span class="sd">        +--------+-------------------------------------+------+------+</span>
<span class="sd">        | FIELD  | DESCRIPTION                         | AKA  | TOP  |</span>
<span class="sd">        +========+=====================================+======+======+</span>
<span class="sd">        | rss    | resident set size                   |  /   | RES  |</span>
<span class="sd">        +--------+-------------------------------------+------+------+</span>
<span class="sd">        | vms    | total program size                  | size | VIRT |</span>
<span class="sd">        +--------+-------------------------------------+------+------+</span>
<span class="sd">        | shared | shared pages (from shared mappings) |  /   | SHR  |</span>
<span class="sd">        +--------+-------------------------------------+------+------+</span>
<span class="sd">        | text   | text (&#39;code&#39;)                       | trs  | CODE |</span>
<span class="sd">        +--------+-------------------------------------+------+------+</span>
<span class="sd">        | lib    | library (unused in Linux 2.6)       | lrs  |   /  |</span>
<span class="sd">        +--------+-------------------------------------+------+------+</span>
<span class="sd">        | data   | data + stack                        | drs  | DATA |</span>
<span class="sd">        +--------+-------------------------------------+------+------+</span>
<span class="sd">        | dirty  | dirty pages (unused in Linux 2.6)   | dt   |   /  |</span>
<span class="sd">        +--------+-------------------------------------+------+------+</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/statm&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">vms</span><span class="p">,</span> <span class="n">rss</span><span class="p">,</span> <span class="n">shared</span><span class="p">,</span> <span class="n">text</span><span class="p">,</span> <span class="n">lib</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">dirty</span> <span class="o">=</span> \
                <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">*</span> <span class="n">_PAGESIZE</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">f</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">()[:</span><span class="mi">7</span><span class="p">]]</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_nt_ext_mem</span><span class="p">(</span><span class="n">rss</span><span class="p">,</span> <span class="n">vms</span><span class="p">,</span> <span class="n">shared</span><span class="p">,</span> <span class="n">text</span><span class="p">,</span> <span class="n">lib</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">dirty</span><span class="p">)</span></div>

    <span class="n">_mmap_base_fields</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;path&#39;</span><span class="p">,</span> <span class="s1">&#39;rss&#39;</span><span class="p">,</span> <span class="s1">&#39;size&#39;</span><span class="p">,</span> <span class="s1">&#39;pss&#39;</span><span class="p">,</span> <span class="s1">&#39;shared_clean&#39;</span><span class="p">,</span>
                         <span class="s1">&#39;shared_dirty&#39;</span><span class="p">,</span> <span class="s1">&#39;private_clean&#39;</span><span class="p">,</span> <span class="s1">&#39;private_dirty&#39;</span><span class="p">,</span>
                         <span class="s1">&#39;referenced&#39;</span><span class="p">,</span> <span class="s1">&#39;anonymous&#39;</span><span class="p">,</span> <span class="s1">&#39;swap&#39;</span><span class="p">,</span> <span class="p">]</span>
    <span class="n">nt_mmap_grouped</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">namedtuple</span><span class="p">(</span>
        <span class="s1">&#39;mmap&#39;</span><span class="p">,</span> <span class="s1">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">_mmap_base_fields</span><span class="p">)</span>
    <span class="p">)</span>
    <span class="n">nt_mmap_ext</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">namedtuple</span><span class="p">(</span>
        <span class="s1">&#39;mmap&#39;</span><span class="p">,</span> <span class="s1">&#39;addr perms &#39;</span> <span class="o">+</span> <span class="s1">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">_mmap_base_fields</span><span class="p">)</span>
    <span class="p">)</span>

<div class="viewcode-block" id="Process.get_memory_maps"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.Process.get_memory_maps">[docs]</a>    <span class="k">def</span> <span class="nf">get_memory_maps</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get memory map (from /proc smaps file)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Return process&#39;s mapped memory regions as a list of nameduples.</span>
        <span class="c1"># Fields are explained in &#39;man proc&#39;; here is an updated (Apr 2012)</span>
        <span class="c1"># version: http://goo.gl/fmebo</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s1">&#39;/proc/</span><span class="si">%s</span><span class="s1">/smaps&#39;</span> <span class="o">%</span> <span class="n">os</span><span class="o">.</span><span class="n">getpid</span><span class="p">()):</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="s2">&quot;couldn&#39;t find /proc/</span><span class="si">%s</span><span class="s2">/smaps; kernel &lt; 2.6.14 or CONFIG_MMU &quot;</span> \
                  <span class="s2">&quot;kernel configuration option is not enabled&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>

        <span class="n">f</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/smaps&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
            <span class="n">first_line</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
            <span class="n">current_block</span> <span class="o">=</span> <span class="p">[</span><span class="n">first_line</span><span class="p">]</span>

            <span class="k">def</span> <span class="nf">get_blocks</span><span class="p">():</span>
                <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">                internal get blocks  for get_memory_maps</span>
<span class="sd">                &quot;&quot;&quot;</span>
                <span class="n">data</span> <span class="o">=</span> <span class="p">{}</span>
                <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
                    <span class="n">fields</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="n">fields</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;:&#39;</span><span class="p">):</span>
                        <span class="c1"># new block section</span>
                        <span class="k">yield</span> <span class="p">(</span><span class="n">current_block</span><span class="o">.</span><span class="n">pop</span><span class="p">(),</span> <span class="n">data</span><span class="p">)</span>
                        <span class="n">current_block</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="k">try</span><span class="p">:</span>
                            <span class="n">data</span><span class="p">[</span><span class="n">fields</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">fields</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">*</span> <span class="mi">1024</span>
                        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                            <span class="k">if</span> <span class="n">fields</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;VmFlags:&#39;</span><span class="p">):</span>
                                <span class="c1"># see issue # 369</span>
                                <span class="k">continue</span>
                            <span class="k">else</span><span class="p">:</span>
                                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;don&#39;t know how to interpret&quot;</span>
                                                 <span class="s2">&quot; line </span><span class="si">%r</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">line</span><span class="p">)</span>
                <span class="k">yield</span> <span class="p">(</span><span class="n">current_block</span><span class="o">.</span><span class="n">pop</span><span class="p">(),</span> <span class="n">data</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">first_line</span><span class="p">:</span>  <span class="c1"># smaps file can be empty</span>
                <span class="k">for</span> <span class="n">header</span><span class="p">,</span> <span class="n">data</span> <span class="ow">in</span> <span class="n">get_blocks</span><span class="p">():</span>
                    <span class="n">hfields</span> <span class="o">=</span> <span class="n">header</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">addr</span><span class="p">,</span> <span class="n">perms</span><span class="p">,</span> <span class="n">offset</span><span class="p">,</span> <span class="n">dev</span><span class="p">,</span> <span class="n">inode</span><span class="p">,</span> <span class="n">path</span> <span class="o">=</span> <span class="n">hfields</span>
                    <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                        <span class="n">addr</span><span class="p">,</span> <span class="n">perms</span><span class="p">,</span> <span class="n">offset</span><span class="p">,</span> <span class="n">dev</span><span class="p">,</span> <span class="n">inode</span><span class="p">,</span> <span class="n">path</span> <span class="o">=</span> <span class="n">hfields</span> <span class="o">+</span> <span class="p">[</span><span class="s1">&#39;&#39;</span><span class="p">]</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="n">path</span><span class="p">:</span>
                        <span class="n">path</span> <span class="o">=</span> <span class="s1">&#39;[anon]&#39;</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
                    <span class="k">yield</span> <span class="p">(</span><span class="n">addr</span><span class="p">,</span> <span class="n">perms</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span>
                           <span class="n">data</span><span class="p">[</span><span class="s1">&#39;Rss:&#39;</span><span class="p">],</span>
                           <span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;Size:&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span>
                           <span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;Pss:&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span>
                           <span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;Shared_Clean:&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span>
                           <span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;Shared_Dirty:&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span>
                           <span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;Private_Clean:&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span>
                           <span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;Private_Dirty:&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span>
                           <span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;Referenced:&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span>
                           <span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;Anonymous:&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span>
                           <span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;Swap:&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span>
            <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="k">except</span> <span class="ne">EnvironmentError</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="c1"># XXX - Can&#39;t use wrap_exceptions decorator as we&#39;re</span>
            <span class="c1"># returning a generator;  this probably needs some</span>
            <span class="c1"># refactoring in order to avoid this code duplication.</span>
            <span class="k">if</span> <span class="n">f</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
            <span class="n">err</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">()[</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">err</span><span class="o">.</span><span class="n">errno</span> <span class="ow">in</span> <span class="p">(</span><span class="n">errno</span><span class="o">.</span><span class="n">ENOENT</span><span class="p">,</span> <span class="n">errno</span><span class="o">.</span><span class="n">ESRCH</span><span class="p">):</span>
                <span class="k">raise</span> <span class="n">cup</span><span class="o">.</span><span class="n">err</span><span class="o">.</span><span class="n">NoSuchProcess</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_process_name</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">err</span><span class="o">.</span><span class="n">errno</span> <span class="ow">in</span> <span class="p">(</span><span class="n">errno</span><span class="o">.</span><span class="n">EPERM</span><span class="p">,</span> <span class="n">errno</span><span class="o">.</span><span class="n">EACCES</span><span class="p">):</span>
                <span class="k">raise</span> <span class="n">cup</span><span class="o">.</span><span class="n">err</span><span class="o">.</span><span class="n">AccessDenied</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_process_name</span><span class="p">)</span>
            <span class="k">raise</span> <span class="n">error</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">f</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
            <span class="k">raise</span> <span class="n">error</span>
        <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span></div>

<div class="viewcode-block" id="Process.get_process_cwd"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.Process.get_process_cwd">[docs]</a>    <span class="nd">@wrap_exceptions</span>
    <span class="k">def</span> <span class="nf">get_process_cwd</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get process current working direcotry</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">readlink</span><span class="p">(</span><span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/cwd&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\x00</span><span class="s1">&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span></div>

    <span class="n">_nt_ctxsw</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">namedtuple</span><span class="p">(</span>
        <span class="s1">&#39;voluntary_ctxt_switches&#39;</span><span class="p">,</span>
        <span class="p">[</span>
            <span class="s1">&#39;vol&#39;</span><span class="p">,</span>
            <span class="s1">&#39;unvol&#39;</span>
        <span class="p">]</span>
    <span class="p">)</span>
<div class="viewcode-block" id="Process.get_num_ctx_switches"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.Process.get_num_ctx_switches">[docs]</a>    <span class="nd">@wrap_exceptions</span>
    <span class="k">def</span> <span class="nf">get_num_ctx_switches</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get process context switch info (from /proc status file), return</span>
<span class="sd">        with a namedtuple (vol, unvol)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">vol</span> <span class="o">=</span> <span class="n">unvol</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/status&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;voluntary_ctxt_switches&quot;</span><span class="p">):</span>
                    <span class="n">vol</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">1</span><span class="p">])</span>
                <span class="k">elif</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;nonvoluntary_ctxt_switches&quot;</span><span class="p">):</span>
                    <span class="n">unvol</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">1</span><span class="p">])</span>
                <span class="k">if</span> <span class="n">vol</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">unvol</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_nt_ctxsw</span><span class="p">(</span><span class="n">vol</span><span class="p">,</span> <span class="n">unvol</span><span class="p">)</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span>
                <span class="s2">&quot;&#39;voluntary_ctxt_switches&#39; and &#39;nonvoluntary_ctxt_switches&#39;&quot;</span>
                <span class="s2">&quot;fields were not found in /proc/</span><span class="si">%s</span><span class="s2">/status; the kernel is &quot;</span>
                <span class="s2">&quot;probably older than 2.6.23&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span></div>

<div class="viewcode-block" id="Process.get_process_num_threads"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.Process.get_process_num_threads">[docs]</a>    <span class="nd">@wrap_exceptions</span>
    <span class="k">def</span> <span class="nf">get_process_num_threads</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get threads num of this process</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/status&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;Threads:&quot;</span><span class="p">):</span>
                    <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">1</span><span class="p">])</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;line not found&quot;</span><span class="p">)</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span></div>

    <span class="n">_thread_tuple</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">namedtuple</span><span class="p">(</span>
        <span class="s1">&#39;ntuple&#39;</span><span class="p">,</span>
        <span class="p">[</span>
            <span class="s1">&#39;thread_id&#39;</span><span class="p">,</span>
            <span class="s1">&#39;utime&#39;</span><span class="p">,</span>
            <span class="s1">&#39;stime&#39;</span>
        <span class="p">]</span>
    <span class="p">)</span>

<div class="viewcode-block" id="Process.get_process_threads"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.Process.get_process_threads">[docs]</a>    <span class="nd">@wrap_exceptions</span>
    <span class="k">def</span> <span class="nf">get_process_threads</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get threads that is current using, return with a namedtuple (</span>
<span class="sd">        thread_id, utime, stime)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">thread_ids</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/task&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
        <span class="n">thread_ids</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>
        <span class="n">retlist</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">hit_enoent</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">for</span> <span class="n">thread_id</span> <span class="ow">in</span> <span class="n">thread_ids</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/task/</span><span class="si">%s</span><span class="s2">/stat&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">,</span> <span class="n">thread_id</span><span class="p">))</span>
            <span class="k">except</span> <span class="ne">EnvironmentError</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
                <span class="n">err</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">()[</span><span class="mi">1</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">err</span><span class="o">.</span><span class="n">errno</span> <span class="o">==</span> <span class="n">errno</span><span class="o">.</span><span class="n">ENOENT</span><span class="p">:</span>
                    <span class="c1"># no such file or directory; it means thread</span>
                    <span class="c1"># disappeared on us</span>
                    <span class="n">hit_enoent</span> <span class="o">=</span> <span class="kc">True</span>
                    <span class="k">continue</span>
                <span class="k">raise</span> <span class="n">error</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">st</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
            <span class="k">finally</span><span class="p">:</span>
                <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
            <span class="c1"># ignore the first two values (&quot;pid (exe)&quot;)</span>
            <span class="n">st</span> <span class="o">=</span> <span class="n">st</span><span class="p">[</span><span class="n">st</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;)&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="mi">2</span><span class="p">:]</span>
            <span class="n">values</span> <span class="o">=</span> <span class="n">st</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">)</span>
            <span class="n">utime</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">values</span><span class="p">[</span><span class="mi">11</span><span class="p">])</span> <span class="o">/</span> <span class="n">_CLOCK_TICKS</span>
            <span class="n">stime</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">values</span><span class="p">[</span><span class="mi">12</span><span class="p">])</span> <span class="o">/</span> <span class="n">_CLOCK_TICKS</span>
            <span class="n">ntuple</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_thread_tuple</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">thread_id</span><span class="p">),</span> <span class="n">utime</span><span class="p">,</span> <span class="n">stime</span><span class="p">)</span>
            <span class="n">retlist</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ntuple</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">hit_enoent</span><span class="p">:</span>
            <span class="c1"># raise NSP if the process disappeared on us</span>
            <span class="n">os</span><span class="o">.</span><span class="n">stat</span><span class="p">(</span><span class="s1">&#39;/proc/</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">retlist</span></div>

<div class="viewcode-block" id="Process.get_process_nice"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.Process.get_process_nice">[docs]</a>    <span class="nd">@wrap_exceptions</span>
    <span class="k">def</span> <span class="nf">get_process_nice</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get process nice</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;/proc/</span><span class="si">%s</span><span class="s1">/stat&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
            <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">18</span><span class="p">])</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span></div>

<div class="viewcode-block" id="Process.get_process_status"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.Process.get_process_status">[docs]</a>    <span class="nd">@wrap_exceptions</span>
    <span class="k">def</span> <span class="nf">get_process_status</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get status of the current process (info from /proc/xxx/status)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/status&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;State:&quot;</span><span class="p">):</span>
                    <span class="n">letter</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">1</span><span class="p">]</span>
                    <span class="c1"># XXX is &#39;?&#39; legit? (we&#39;re not supposed to return</span>
                    <span class="c1"># it anyway)</span>
                    <span class="k">return</span> <span class="n">_PROC_STATUSES</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">letter</span><span class="p">,</span> <span class="s1">&#39;?&#39;</span><span class="p">)</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span></div>

    <span class="n">_nt_openfile</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">namedtuple</span><span class="p">(</span>
        <span class="s1">&#39;nt_openfile&#39;</span><span class="p">,</span>
        <span class="p">[</span>
            <span class="s1">&#39;file&#39;</span><span class="p">,</span>
            <span class="s1">&#39;fd&#39;</span>
        <span class="p">]</span>
    <span class="p">)</span>

<div class="viewcode-block" id="Process.get_open_files"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.Process.get_open_files">[docs]</a>    <span class="nd">@wrap_exceptions</span>
    <span class="k">def</span> <span class="nf">get_open_files</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get opened file info</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">retlist</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">files</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/fd&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
        <span class="n">hit_enoent</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">for</span> <span class="n">fd</span> <span class="ow">in</span> <span class="n">files</span><span class="p">:</span>
            <span class="n">file</span> <span class="o">=</span> <span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/fd/</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">,</span> <span class="n">fd</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">islink</span><span class="p">(</span><span class="n">file</span><span class="p">):</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">file</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">readlink</span><span class="p">(</span><span class="n">file</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
                    <span class="c1"># ENOENT == file which is gone in the meantime</span>
                    <span class="n">err</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">()[</span><span class="mi">1</span><span class="p">]</span>
                    <span class="k">if</span> <span class="n">err</span><span class="o">.</span><span class="n">errno</span> <span class="o">==</span> <span class="n">errno</span><span class="o">.</span><span class="n">ENOENT</span><span class="p">:</span>
                        <span class="n">hit_enoent</span> <span class="o">=</span> <span class="kc">True</span>
                        <span class="k">continue</span>
                    <span class="k">raise</span> <span class="n">error</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="c1"># If file is not an absolute path there&#39;s no way</span>
                    <span class="c1"># to tell whether it&#39;s a regular file or not,</span>
                    <span class="c1"># so we skip it. A regular file is always supposed</span>
                    <span class="c1"># to be absolutized though.</span>
                    <span class="k">if</span> <span class="n">file</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">file</span><span class="p">):</span>
                        <span class="n">ntuple</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_nt_openfile</span><span class="p">(</span><span class="n">file</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">fd</span><span class="p">))</span>
                        <span class="n">retlist</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ntuple</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">hit_enoent</span><span class="p">:</span>
            <span class="c1"># raise NSP if the process disappeared on us</span>
            <span class="n">os</span><span class="o">.</span><span class="n">stat</span><span class="p">(</span><span class="s1">&#39;/proc/</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">retlist</span></div>

    <span class="n">_nt_connection</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">namedtuple</span><span class="p">(</span>
        <span class="s1">&#39;nt_connection&#39;</span><span class="p">,</span>
        <span class="p">[</span>
            <span class="s1">&#39;fd&#39;</span><span class="p">,</span>
            <span class="s1">&#39;family&#39;</span><span class="p">,</span>
            <span class="s1">&#39;type&#39;</span><span class="p">,</span>
            <span class="s1">&#39;laddr&#39;</span><span class="p">,</span>
            <span class="s1">&#39;raddr&#39;</span><span class="p">,</span>
            <span class="s1">&#39;status&#39;</span>
        <span class="p">]</span>
    <span class="p">)</span>

    <span class="k">def</span> <span class="nf">_process_for_connections</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">inodes</span><span class="p">,</span> <span class="n">file</span><span class="p">,</span> <span class="n">family</span><span class="p">,</span> <span class="n">type_</span><span class="p">):</span>
        <span class="n">retlist</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">file</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">IOError</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="c1"># IPv6 not supported on this platform</span>
            <span class="n">err</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">()[</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">err</span><span class="o">.</span><span class="n">errno</span> <span class="o">==</span> <span class="n">errno</span><span class="o">.</span><span class="n">ENOENT</span> <span class="ow">and</span> <span class="n">file</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;6&#39;</span><span class="p">):</span>
                <span class="k">return</span> <span class="p">[]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">error</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">f</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>  <span class="c1"># skip the first line</span>
            <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
                <span class="c1"># IPv4 / IPv6</span>
                <span class="k">if</span> <span class="n">family</span> <span class="ow">in</span> <span class="p">(</span><span class="n">socket</span><span class="o">.</span><span class="n">AF_INET</span><span class="p">,</span> <span class="n">socket</span><span class="o">.</span><span class="n">AF_INET6</span><span class="p">):</span>
                    <span class="n">_</span><span class="p">,</span> <span class="n">laddr</span><span class="p">,</span> <span class="n">raddr</span><span class="p">,</span> <span class="n">status</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">inode</span> <span class="o">=</span> \
                        <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[:</span><span class="mi">10</span><span class="p">]</span>
                    <span class="k">if</span> <span class="n">inode</span> <span class="ow">in</span> <span class="n">inodes</span><span class="p">:</span>
                        <span class="n">laddr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_decode_address</span><span class="p">(</span><span class="n">laddr</span><span class="p">,</span> <span class="n">family</span><span class="p">)</span>
                        <span class="n">raddr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_decode_address</span><span class="p">(</span><span class="n">raddr</span><span class="p">,</span> <span class="n">family</span><span class="p">)</span>
                        <span class="k">if</span> <span class="n">type_</span> <span class="o">==</span> <span class="n">socket</span><span class="o">.</span><span class="n">SOCK_STREAM</span><span class="p">:</span>
                            <span class="n">status</span> <span class="o">=</span> <span class="n">_TCP_STATUSES</span><span class="p">[</span><span class="n">status</span><span class="p">]</span>
                        <span class="k">else</span><span class="p">:</span>
                            <span class="n">status</span> <span class="o">=</span> <span class="n">_CONN_NONE</span>
                        <span class="n">fd</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">inodes</span><span class="p">[</span><span class="n">inode</span><span class="p">])</span>
                        <span class="n">conn</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_nt_connection</span><span class="p">(</span>
                            <span class="n">fd</span><span class="p">,</span> <span class="n">family</span><span class="p">,</span> <span class="n">type_</span><span class="p">,</span> <span class="n">laddr</span><span class="p">,</span>
                            <span class="n">raddr</span><span class="p">,</span> <span class="n">status</span>
                        <span class="p">)</span>
                        <span class="n">retlist</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">conn</span><span class="p">)</span>
                <span class="k">elif</span> <span class="n">family</span> <span class="o">==</span> <span class="n">socket</span><span class="o">.</span><span class="n">AF_UNIX</span><span class="p">:</span>
                    <span class="n">tokens</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
                    <span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">type_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">inode</span> <span class="o">=</span> <span class="n">tokens</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">7</span><span class="p">]</span>
                    <span class="k">if</span> <span class="n">inode</span> <span class="ow">in</span> <span class="n">inodes</span><span class="p">:</span>

                        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">tokens</span><span class="p">)</span> <span class="o">==</span> <span class="mi">8</span><span class="p">:</span>
                            <span class="n">path</span> <span class="o">=</span> <span class="n">tokens</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
                        <span class="k">else</span><span class="p">:</span>
                            <span class="n">path</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
                        <span class="n">fd</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">inodes</span><span class="p">[</span><span class="n">inode</span><span class="p">])</span>
                        <span class="n">type_</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">type_</span><span class="p">)</span>
                        <span class="n">conn</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_nt_connection</span><span class="p">(</span>
                            <span class="n">fd</span><span class="p">,</span> <span class="n">family</span><span class="p">,</span> <span class="n">type_</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span>
                            <span class="kc">None</span><span class="p">,</span> <span class="n">_CONN_NONE</span>
                        <span class="p">)</span>
                        <span class="n">retlist</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">conn</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="n">family</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">retlist</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

<div class="viewcode-block" id="Process.get_connections"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.Process.get_connections">[docs]</a>    <span class="nd">@wrap_exceptions</span>
    <span class="k">def</span> <span class="nf">get_connections</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">kind</span><span class="o">=</span><span class="s1">&#39;inet&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get network connection info, each item contains a namedtuple</span>
<span class="sd">        (fd family type laddr raddr status)</span>

<span class="sd">        :param kind:</span>
<span class="sd">            kind=&#39;inet&#39; by default</span>
<span class="sd">        :return:</span>
<span class="sd">            a list of network connection info</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c1"># Return connections opened by process as a list of namedtuples.</span>
        <span class="c1"># The kind parameter filters for connections that fit the following</span>
        <span class="c1"># criteria:</span>

        <span class="c1"># Kind Value      Number of connections using</span>
        <span class="c1"># inet            IPv4 and IPv6</span>
        <span class="c1"># inet4           IPv4</span>
        <span class="c1"># inet6           IPv6</span>
        <span class="c1"># tcp             TCP</span>
        <span class="c1"># tcp4            TCP over IPv4</span>
        <span class="c1"># tcp6            TCP over IPv6</span>
        <span class="c1"># udp             UDP</span>
        <span class="c1"># udp4            UDP over IPv4</span>
        <span class="c1"># udp6            UDP over IPv6</span>
        <span class="c1"># all             the sum of all the possible families and protocols</span>
        <span class="c1"># Note: in case of UNIX sockets we&#39;re only able to determine the</span>
        <span class="c1"># local bound path while the remote endpoint is not retrievable:</span>
        <span class="c1"># http://goo.gl/R3GHM</span>
        <span class="n">inodes</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c1"># os.listdir() is gonna raise a lot of access denied</span>
        <span class="c1"># exceptions in case of unprivileged user; that&#39;s fine:</span>
        <span class="c1"># lsof does the same so it&#39;s unlikely that we can to better.</span>
        <span class="k">for</span> <span class="n">fd</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/fd&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">):</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">inode</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">readlink</span><span class="p">(</span><span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/fd/</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">,</span> <span class="n">fd</span><span class="p">))</span>
            <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="k">if</span> <span class="n">inode</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;socket:[&#39;</span><span class="p">):</span>
                <span class="c1"># the process is using a socket</span>
                <span class="n">inode</span> <span class="o">=</span> <span class="n">inode</span><span class="p">[</span><span class="mi">8</span><span class="p">:][:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
                <span class="n">inodes</span><span class="p">[</span><span class="n">inode</span><span class="p">]</span> <span class="o">=</span> <span class="n">fd</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">inodes</span><span class="p">:</span>
            <span class="c1"># no connections for this process</span>
            <span class="k">return</span> <span class="p">[]</span>

        <span class="n">tcp4</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;tcp&quot;</span><span class="p">,</span> <span class="n">socket</span><span class="o">.</span><span class="n">AF_INET</span><span class="p">,</span> <span class="n">socket</span><span class="o">.</span><span class="n">SOCK_STREAM</span><span class="p">)</span>
        <span class="n">tcp6</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;tcp6&quot;</span><span class="p">,</span> <span class="n">socket</span><span class="o">.</span><span class="n">AF_INET6</span><span class="p">,</span> <span class="n">socket</span><span class="o">.</span><span class="n">SOCK_STREAM</span><span class="p">)</span>
        <span class="n">udp4</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;udp&quot;</span><span class="p">,</span> <span class="n">socket</span><span class="o">.</span><span class="n">AF_INET</span><span class="p">,</span> <span class="n">socket</span><span class="o">.</span><span class="n">SOCK_DGRAM</span><span class="p">)</span>
        <span class="n">udp6</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;udp6&quot;</span><span class="p">,</span> <span class="n">socket</span><span class="o">.</span><span class="n">AF_INET6</span><span class="p">,</span> <span class="n">socket</span><span class="o">.</span><span class="n">SOCK_DGRAM</span><span class="p">)</span>
        <span class="n">unix</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;unix&quot;</span><span class="p">,</span> <span class="n">socket</span><span class="o">.</span><span class="n">AF_UNIX</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>

        <span class="n">tmap</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s2">&quot;all&quot;</span><span class="p">:</span> <span class="p">(</span><span class="n">tcp4</span><span class="p">,</span> <span class="n">tcp6</span><span class="p">,</span> <span class="n">udp4</span><span class="p">,</span> <span class="n">udp6</span><span class="p">,</span> <span class="n">unix</span><span class="p">),</span>
            <span class="s2">&quot;tcp&quot;</span><span class="p">:</span> <span class="p">(</span><span class="n">tcp4</span><span class="p">,</span> <span class="n">tcp6</span><span class="p">),</span>
            <span class="s2">&quot;tcp4&quot;</span><span class="p">:</span> <span class="p">(</span><span class="n">tcp4</span><span class="p">,</span> <span class="p">),</span>
            <span class="s2">&quot;tcp6&quot;</span><span class="p">:</span> <span class="p">(</span><span class="n">tcp6</span><span class="p">,</span> <span class="p">),</span>
            <span class="s2">&quot;udp&quot;</span><span class="p">:</span> <span class="p">(</span><span class="n">udp4</span><span class="p">,</span> <span class="n">udp6</span><span class="p">),</span>
            <span class="s2">&quot;udp4&quot;</span><span class="p">:</span> <span class="p">(</span><span class="n">udp4</span><span class="p">,</span> <span class="p">),</span>
            <span class="s2">&quot;udp6&quot;</span><span class="p">:</span> <span class="p">(</span><span class="n">udp6</span><span class="p">,</span> <span class="p">),</span>
            <span class="s2">&quot;unix&quot;</span><span class="p">:</span> <span class="p">(</span><span class="n">unix</span><span class="p">,</span> <span class="p">),</span>
            <span class="s2">&quot;inet&quot;</span><span class="p">:</span> <span class="p">(</span><span class="n">tcp4</span><span class="p">,</span> <span class="n">tcp6</span><span class="p">,</span> <span class="n">udp4</span><span class="p">,</span> <span class="n">udp6</span><span class="p">),</span>
            <span class="s2">&quot;inet4&quot;</span><span class="p">:</span> <span class="p">(</span><span class="n">tcp4</span><span class="p">,</span> <span class="n">udp4</span><span class="p">),</span>
            <span class="s2">&quot;inet6&quot;</span><span class="p">:</span> <span class="p">(</span><span class="n">tcp6</span><span class="p">,</span> <span class="n">udp6</span><span class="p">),</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="n">kind</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">tmap</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;invalid </span><span class="si">%r</span><span class="s2"> kind argument; choose between </span><span class="si">%s</span><span class="s2">&quot;</span>
                             <span class="o">%</span> <span class="p">(</span><span class="n">kind</span><span class="p">,</span> <span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="nb">repr</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">tmap</span><span class="p">])))</span>
        <span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">f</span><span class="p">,</span> <span class="n">family</span><span class="p">,</span> <span class="n">type_</span> <span class="ow">in</span> <span class="n">tmap</span><span class="p">[</span><span class="n">kind</span><span class="p">]:</span>
            <span class="n">ret</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_process_for_connections</span><span class="p">(</span>
                <span class="n">inodes</span><span class="p">,</span> <span class="s2">&quot;/proc/net/</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">f</span><span class="p">,</span> <span class="n">family</span><span class="p">,</span> <span class="n">type_</span>
            <span class="p">)</span>
        <span class="c1"># raise NSP if the process disappeared on us</span>
        <span class="n">os</span><span class="o">.</span><span class="n">stat</span><span class="p">(</span><span class="s1">&#39;/proc/</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">ret</span></div>

<div class="viewcode-block" id="Process.get_num_fds"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.Process.get_num_fds">[docs]</a>    <span class="nd">@wrap_exceptions</span>
    <span class="k">def</span> <span class="nf">get_num_fds</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get opened file descriptor num</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/fd&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">))</span></div>

<div class="viewcode-block" id="Process.get_process_ppid"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.Process.get_process_ppid">[docs]</a>    <span class="nd">@wrap_exceptions</span>
    <span class="k">def</span> <span class="nf">get_process_ppid</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get parent process id</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/status&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;PPid:&quot;</span><span class="p">):</span>
                    <span class="c1"># PPid: nnnn</span>
                    <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">1</span><span class="p">])</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;line not found&quot;</span><span class="p">)</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span></div>

    <span class="n">_nt_uids</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">namedtuple</span><span class="p">(</span>
        <span class="s1">&#39;nt_uids&#39;</span><span class="p">,</span>
        <span class="p">[</span>
            <span class="s1">&#39;real&#39;</span><span class="p">,</span>
            <span class="s1">&#39;effective&#39;</span><span class="p">,</span>
            <span class="s1">&#39;saved&#39;</span>
        <span class="p">]</span>
    <span class="p">)</span>

<div class="viewcode-block" id="Process.get_process_uids"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.Process.get_process_uids">[docs]</a>    <span class="nd">@wrap_exceptions</span>
    <span class="k">def</span> <span class="nf">get_process_uids</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get uid info of the process, will return a namedtuple</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/status&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;Uid:&#39;</span><span class="p">):</span>
                    <span class="n">_</span><span class="p">,</span> <span class="n">real</span><span class="p">,</span> <span class="n">effective</span><span class="p">,</span> <span class="n">saved</span><span class="p">,</span> <span class="n">fs</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
                    <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_nt_uids</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">real</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="n">effective</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="n">saved</span><span class="p">))</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;line not found&quot;</span><span class="p">)</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span></div>

    <span class="n">_nt_gids</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">namedtuple</span><span class="p">(</span>
        <span class="s1">&#39;nt_gids&#39;</span><span class="p">,</span>
        <span class="p">[</span>
            <span class="s1">&#39;real&#39;</span><span class="p">,</span>
            <span class="s1">&#39;effective&#39;</span><span class="p">,</span>
            <span class="s1">&#39;saved&#39;</span>
        <span class="p">]</span>
    <span class="p">)</span>

<div class="viewcode-block" id="Process.get_process_gids"><a class="viewcode-back" href="../../../../cup.res/#cup.res.linux.Process.get_process_gids">[docs]</a>    <span class="nd">@wrap_exceptions</span>
    <span class="k">def</span> <span class="nf">get_process_gids</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get process gid, namedtuple will be returned</span>
<span class="sd">        (with attrs .real .effective .saved)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;/proc/</span><span class="si">%s</span><span class="s2">/status&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pid</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;Gid:&#39;</span><span class="p">):</span>
                    <span class="n">_</span><span class="p">,</span> <span class="n">real</span><span class="p">,</span> <span class="n">effective</span><span class="p">,</span> <span class="n">saved</span><span class="p">,</span> <span class="n">fs</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
                    <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_nt_gids</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">real</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="n">effective</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="n">saved</span><span class="p">))</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;line not found&quot;</span><span class="p">)</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span></div>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_decode_address</span><span class="p">(</span><span class="n">addr</span><span class="p">,</span> <span class="n">family</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Accept an &quot;ip:port&quot; address as displayed in /proc/net/*</span>
<span class="sd">        and convert it into a human readable form, like:</span>

<span class="sd">        &quot;0500000A:0016&quot; -&gt; (&quot;10.0.0.5&quot;, 22)</span>
<span class="sd">        &quot;0000000000000000FFFF00000100007F:9E49&quot; -&gt; (&quot;::ffff:127.0.0.1&quot;, 40521)</span>

<span class="sd">        The IP address portion is a little or big endian four-byte</span>
<span class="sd">        hexadecimal number; that is, the least significant byte is listed</span>
<span class="sd">        first, so we need to reverse the order of the bytes to convert it</span>
<span class="sd">        to an IP address.</span>
<span class="sd">        The port is represented as a two-byte hexadecimal number.</span>

<span class="sd">        Reference:</span>
<span class="sd">        http://linuxdevcenter.com/pub/a/linux/2000/11/16/LinuxAdmin.html</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">ip</span><span class="p">,</span> <span class="n">port</span> <span class="o">=</span> <span class="n">addr</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;:&#39;</span><span class="p">)</span>
        <span class="n">port</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">port</span><span class="p">,</span> <span class="mi">16</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span> <span class="o">&gt;=</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">0</span><span class="p">):</span>
            <span class="n">ip</span> <span class="o">=</span> <span class="n">ip</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;ascii&#39;</span><span class="p">)</span>
        <span class="c1"># this usually refers to a local socket in listen mode with</span>
        <span class="c1"># no end-points connected</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">port</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">()</span>
        <span class="k">if</span> <span class="n">family</span> <span class="o">==</span> <span class="n">socket</span><span class="o">.</span><span class="n">AF_INET</span><span class="p">:</span>
            <span class="c1"># see: http://code.google.com/p/psutil/issues/detail?id=201</span>
            <span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">byteorder</span> <span class="o">==</span> <span class="s1">&#39;little&#39;</span><span class="p">:</span>
                <span class="n">ip</span> <span class="o">=</span> <span class="n">socket</span><span class="o">.</span><span class="n">inet_ntop</span><span class="p">(</span><span class="n">family</span><span class="p">,</span> <span class="n">base64</span><span class="o">.</span><span class="n">b16decode</span><span class="p">(</span><span class="n">ip</span><span class="p">)[::</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">ip</span> <span class="o">=</span> <span class="n">socket</span><span class="o">.</span><span class="n">inet_ntop</span><span class="p">(</span><span class="n">family</span><span class="p">,</span> <span class="n">base64</span><span class="o">.</span><span class="n">b16decode</span><span class="p">(</span><span class="n">ip</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>  <span class="c1"># IPv6</span>
            <span class="c1"># old version - let&#39;s keep it, just in case...</span>
            <span class="c1"># ip = ip.decode(&#39;hex&#39;)</span>
            <span class="c1"># return socket.inet_ntop(socket.AF_INET6,</span>
            <span class="c1">#          &#39;&#39;.join(ip[i:i+4][::-1] for i in xrange(0, 16, 4)))</span>
            <span class="n">ip</span> <span class="o">=</span> <span class="n">base64</span><span class="o">.</span><span class="n">b16decode</span><span class="p">(</span><span class="n">ip</span><span class="p">)</span>
            <span class="c1"># see: http://code.google.com/p/psutil/issues/detail?id=201</span>
            <span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">byteorder</span> <span class="o">==</span> <span class="s1">&#39;little&#39;</span><span class="p">:</span>
                <span class="n">ip</span> <span class="o">=</span> <span class="n">socket</span><span class="o">.</span><span class="n">inet_ntop</span><span class="p">(</span>
                    <span class="n">socket</span><span class="o">.</span><span class="n">AF_INET6</span><span class="p">,</span>
                    <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&gt;4I&#39;</span><span class="p">,</span> <span class="o">*</span><span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s1">&#39;&lt;4I&#39;</span><span class="p">,</span> <span class="n">ip</span><span class="p">)))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">ip</span> <span class="o">=</span> <span class="n">socket</span><span class="o">.</span><span class="n">inet_ntop</span><span class="p">(</span>
                    <span class="n">socket</span><span class="o">.</span><span class="n">AF_INET6</span><span class="p">,</span>
                    <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s1">&#39;&lt;4I&#39;</span><span class="p">,</span> <span class="o">*</span><span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s1">&#39;&lt;4I&#39;</span><span class="p">,</span> <span class="n">ip</span><span class="p">)))</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">)</span></div>

<span class="k">if</span> <span class="s1">&#39;__main__&#39;</span> <span class="o">==</span> <span class="vm">__name__</span><span class="p">:</span>
    <span class="c1"># system info</span>
    <span class="nb">print</span> <span class="n">get_boottime_since_epoch</span><span class="p">()</span>
    <span class="nb">print</span> <span class="n">get_cpu_nums</span><span class="p">()</span>
    <span class="nb">print</span> <span class="n">get_kernel_version</span><span class="p">()</span>
    <span class="nb">print</span> <span class="n">get_disk_usage_all</span><span class="p">()</span>
    <span class="nb">print</span> <span class="n">get_disk_info</span><span class="p">()</span>

    <span class="c1"># resouce info</span>
    <span class="nb">print</span> <span class="n">get_cpu_usage</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
    <span class="nb">print</span> <span class="n">get_meminfo</span><span class="p">()</span>
    <span class="nb">print</span> <span class="n">get_swapinfo</span><span class="p">()</span>
    <span class="nb">print</span> <span class="n">get_net_through</span><span class="p">(</span><span class="s1">&#39;xgbe0&#39;</span><span class="p">)</span>
    <span class="nb">print</span> <span class="n">get_net_transmit_speed</span><span class="p">(</span><span class="s1">&#39;xgbe0&#39;</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
    <span class="nb">print</span> <span class="n">get_net_recv_speed</span><span class="p">(</span><span class="s1">&#39;xgbe0&#39;</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
    <span class="nb">print</span> <span class="n">net_io_counters</span><span class="p">()</span>

<span class="c1"># vi:set tw=0 ts=4 sw=4 nowrap fdm=indent</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, CUP-DEV.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../../',
            VERSION:'1.7.0',
            LANGUAGE:'en',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../../_static/doctools.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>